ruby-on-rails - 升级到 Ruby on Rails 3.1 : How to solve image loading issues in CSS files?

标签 ruby-on-rails css ruby ruby-on-rails-3 image

我正在使用 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/

相关文章:

mysql - rails 4 : Using ActiveRecord object from one thread in another

ruby - 如何在rp5rc中设置processing_root

ruby-on-rails - 远程服务器上的 Postgres 语言环境错误

ruby-on-rails - 对 Devise Controller 进行子类化后,Rails View 中的实例变量为零

ruby-on-rails - Ruby:从数组中删除第一个元素的最简单方法是什么?

css - 滑动子菜单纯 CSS

javascript - 玩转 HTML 有序列表编号元素符号

ruby-on-rails - 帮助解决 'Missing these required gems: error'

html - 有没有办法在 CSS 2D 光栅化中消除子像素的锯齿?

ruby-on-rails - Ruby/Rails 中的类方法与常量