我正在使用 Ruby on Rails 3.0.10 并将我的 RoR 升级到版本 3.1.0。我正在关注 Upgrading to Rails 3.1 railscast 和时间 07:04 他开始谈论加载图像文件的问题...
简而言之:
[...] This (that is, image loading - more information here) won’t work as our application’s images are no longer in the /public/images directory. The simplest solution might appear to be to change the image’s URL from /images/railscasts_logo.png to /assets/railscasts_logo.png and while this will appear to work in development mode we’ll run into issues in production. [...] We should instead always use the helper methods. This will link the image correctly in both development and production mode. [...]
我的问题出在我的 CSS 文件中,其中有如下语句
.a_class {
...
background-image: url("/images/avatar.png");
...
}
没有正确加载图像文件,这是应该的。
我如何在 Rails 3.1 中解决这个问题?
最佳答案
您可能会查看 railsguides,尤其是 Assets 管道 (http://guides.rubyonrails.org/asset_pipeline.html) 的部分。
在 2.2.2 CSS 和 SASS 部分,您将找到以下解释:
When using the asset pipeline, paths to assets must be re-written and sass-rails provides _url and _path helpers for the following asset classes: image, font, video, audio, javascript, stylesheet.
image-url("rails.png") becomes url(/assets/rails.png) image-path("rails.png") becomes "/assets/rails.png".
The more generic form can also be used but the asset path and class must both be specified:
asset-url("rails.png", image) becomes url(/assets/rails.png) asset-path("rails.png", image) becomes "/assets/rails.png"
还有一种替代方法,即添加 .erb 扩展名而不是默认的 .sass,这在 2.2.1 节中有描述。但无论如何我更喜欢 sass-way。
编辑:
您可能还想查看 this post在 stackoverflow 上。
关于ruby-on-rails - 升级到 Ruby on Rails 3.1 : How to solve image loading issues in CSS files?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7493591/