'Rails 3.1: Determine if asset exists
Is there a built-in way to determine if an asset exists without resorting to File.exists?(File.join(Rails.root, "foo", "bar", "baz")) and that looks through the asset paths.
My app goes and fetches images from a remote server on a Resque queue; until we have the image downloaded I want to serve a placeholder image. Currently I'm using File.exists... but this means hard-coding a path, which sucks, or looking through the configured asset paths. It seems like this should be there already, but I can't find it in the docs.
Solution 1:[1]
Since this is still the top question when searching Google, and since the accepted answer does not work properly in production (at least in some cases), here is the solution that works for me (Rails 4.x to 6.x at least):
def asset_exist?(path)
if Rails.configuration.assets.compile
Rails.application.precompiled_assets.include? path
else
Rails.application.assets_manifest.assets[path].present?
end
end
This is copied from this github issue
Solution 2:[2]
See this answer: https://stackoverflow.com/a/8217598/549252
= Rails.application.assets.find_asset("my_asset.css").nil?
Solution 3:[3]
Please see the answer here for a discussion on why find_asset may not always work:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Community |
| Solution 3 | Community |
