如何在 Rails 3.1 应用程序中处理 CSS 中的图像引用

问题描述:

我刚刚将 Rails 应用升级到 3.1,并且正在使用新的资产管道.管道就位后,您的应用程序中的任何静态引用都必须使用 rails 辅助方法,即 asset_path(...)、img_tag() 等....

I just upgraded my rails app to 3.1 and I'm making use of the new asset pipeline. With the pipeline in place it's imperative that any static references made in your application utilize the rails helper methods, ie, asset_path(...), img_tag(), etc....

问题来了,如何修复我的 css 文件中的静态引用?

Here's the question, how do I fix the static references in my css files?

在 Rails 的 SASS 文件中,有一个 image-url() 函数,你可以像这样使用:

In Rails' SASS files, there's an image-url() function you can use like so:

.page {
    background: #eee image-url('somefile.png');
}

Rails 将在所有资产图像目录中查找该文件,例如 app/assets/images/somefile.png.

Rails will look for that file in all the asset images directories, such as app/assets/images/somefile.png.

查看 sass-rails 的自述文件,了解更多辅助方法以及 Asset Pipeline Rails 指南 以获得进一步的文档和解释.

Check the readme for sass-rails for more helper methods, as well as the Asset Pipeline Rails guide for further documentation and explanation.