问题
ActionView::Template::Error (leagues/lal11.png isn't precompiled):
Errors of this sort happen in my app when a new league is added but the corresponding image hasn't been precompiled.
I understand why this is happening; the asset manifest is missing the relevant path and so rails can't determine the digest for it. However, killing the whole app because of a simple missing image is stupid. Is there a way to catch this specific exception?
回答1:
i could not agree more with you, raising errors for missing images... i think the asset-pipeline is the most crappy piece of code in rails!
i have some ideas on how you could tackle the problem:
you could enable live compilation config.assets.compile = true
you could catch that error in the helper you are using (image_tag
or whatever) and render a default image.
you could use rescue_from
in your controller if the error is not allover your app.
回答2:
Blaming the asset pipeline in this case seems wrong. Its an optional thing that is there to speed up your application. If you're happy with a slower application that will fallback, this is easily configurable in config/environments/production.rb
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
I wouldn't recommend doing this though!
Although one could argue that this isn't a case where an exception error should be raised... the fact is you're pulling an image that doesn't exist.
So there is an error that your code needs to handle. IMO its better in Object Oriented code to fail massively instead of cleverly. This fail creates a nice failure message you can use to fix the code.
I would propose routing through a helper that could handle the error. Something like this which is a variant of this answer
def safe_image_tag(source, options = {})
begin
count = 0
source ||= "blank-profile-md.png"
image_tag(source, options)
rescue Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError
count += 1
source = "blank-profile-md.png"
retry if count < 2
end
end
This will in this one case recover and render your default image.
来源:https://stackoverflow.com/questions/12871503/rails-ignore-actionviewtemplateerror-for-missing-assets