Rails 3.1:确定资产是否存在 [英] Rails 3.1: Determine if asset exists
问题描述
是否有内置方法来确定资产是否存在而无需借助File.exists?(File.join(Rails.root, "foo", "bar", "baz"))
并通过资产路径进行查找.
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.
我的应用程序运行并从Resque队列中的远程服务器获取图像;直到下载完图片,我才想提供一个占位符图片.当前,我正在使用File.exists
...,但这意味着对路径进行硬编码,这很烂,或者正在浏览配置的资产路径.似乎应该已经存在了,但是我在文档中找不到它.
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.
推荐答案
给出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
这篇关于Rails 3.1:确定资产是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!