是否有内置的方式来确定资产是否存在,而不使用File.exists?(File.join(Rails.root, "foo", "bar", "baz"))
并且通过资产路径进行查看。Rails 3.1:确定是否存在资产
我的应用程序从Resque队列中的远程服务器获取图像;直到我们下载了图像,我想为占位符图像提供服务。目前我正在使用File.exists
...但这意味着硬编码路径,这会吸引或查看已配置的资产路径。这似乎应该已经存在,但我无法在文档中找到它。
是否有内置的方式来确定资产是否存在,而不使用File.exists?(File.join(Rails.root, "foo", "bar", "baz"))
并且通过资产路径进行查看。Rails 3.1:确定是否存在资产
我的应用程序从Resque队列中的远程服务器获取图像;直到我们下载了图像,我想为占位符图像提供服务。目前我正在使用File.exists
...但这意味着硬编码路径,这会吸引或查看已配置的资产路径。这似乎应该已经存在,但我无法在文档中找到它。
鉴于app/assets/images/lolshirts/theme/bg-header.png
图像,
Rails.application.assets.find_asset 'lolshirts/theme/bg-header.png'
=> #> Sprockets::StaticAsset:0x80c388ec pathname="/Users/joevandyk/projects/tanga/sites/lolshirts/app/assets/images/lolshirts/theme/bg-header.png", mtime=2011-10-07 12:34:48 -0700, digest="a63cc84aca38e2172ae25de3d837c71a">
Rails.application.assets.find_asset 'notthere.png'
=> nil
看到这个答案: https://stackoverflow.com/a/8217598/549252
= Rails.application.assets.find_asset("my_asset.css").nil?
由于这仍然是搜索Google时遇到的最大问题,并且由于接受的答案在生产中无法正常工作(至少在某些情况下),下面是适用于我的解决方案(Rails 4.2.5.1):
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
看起来像乔的回答是喋喋不休复制。你会标记它是正确的? –