如何在 Rails 3.1 中获取资产的绝对 URL?
image_path
仅返回路径(无主机).
image_path
returns a path only (no host).
url_for
助手不会接受单个路径,因此像 url_for(image_path('image.png'))
这样的东西将不起作用.虽然 ActionDispatch::Http::Url.url_for
中的内部 url_for
似乎可以解决问题(source),似乎没有它的公共接口.
The url_for
helper won't accept a single path, so something like url_for(image_path('image.png'))
wouldn't work. While the internal url_for
in ActionDispatch::Http::Url.url_for
would appear to do the trick (source), there doesn't seem to be a public interface for it.
我应该怎么做?最终,有一个像 image_url
这样的函数会很好,它像 url_for
一样用于路由,这样我就可以调用 image_url('image.png')
并获取给定所有 default_url_options
的绝对 URL.
How should I go about doing it? Ultimately, it'd be nice to have a function like image_url
that works like url_for
does for routes so that I could call image_url('image.png')
and get the absolute URL given all of the default_url_options
.
最近看来,sass-rails
现在可以在 scss 中解释 image_url
命令以预期的方式文件,解析到有问题的图像的最终位置.
It would appear that as of recently, sass-rails
now interprets the image_url
command within a scss file in the expected manner, resolving to the final location of the image in question.