如何在Rails 4.0中引用生产中的CSS背景图像?

hlh*_*hlh 10 ruby-on-rails-3

我将应用程序从3.2.12切换到4.0.0.rc1.当我之后在制作中看到它时,背景图像消失了.我的其他图像资源都没有问题.

在我的日志中,我看到所有成功渲染的图像都有一个摘要添加到它们的末尾,如下所示:

2013-05-12T19:57:05.856277+00:00 heroku[router]: at=info method=GET path=/asset/explore-9ec2a1cfd4784133755637f6ef6d5673.png host=xxx.herokuapp.com fwd="69.140.148.75" dyno=web.1 connect=3ms service=5ms status=200 bytes=4064
Run Code Online (Sandbox Code Playgroud)

我的不成功的背景图片没有摘要,加上404响应代码:

2013-05-12T19:57:05.736354+00:00 heroku[router]: at=info method=GET path=/assets/background.png host=xxxx.herokuapp.com fwd="69.140.148.75" dyno=web.1 connect=2ms service=7ms status=404 bytes=728
Run Code Online (Sandbox Code Playgroud)

在production.rb文件中,有一个配置行,用于缓存目的:

# Generate digests for assets URLs
config.assets.digest = true
Run Code Online (Sandbox Code Playgroud)

这是后台的CSS:

body {
  background-image: url('background.png');
  background-attachment: fixed;
  background-position: 100% 100%;
  background-repeat: no-repeat;
}
Run Code Online (Sandbox Code Playgroud)

我得出结论,我的CSS文件试图获取一个不存在的图像URL,因为它引用了普通资产("background.png")而没有最后的哈希.这只是CSS中图像的一个问题; 我的.erb文件中引用的所有图像都可以.那么如何在我的CSS中引用这个资产而不用硬编码摘要呢?有没有解决方法?

谢谢阅读.

aro*_*ero 21

使用asset-url.Rails将对此进行预处理并展开正确的URL.

body {
  background-image: asset-url('background.png');
  background-attachment: fixed;
  background-position: 100% 100%;
  background-repeat: no-repeat;
}
Run Code Online (Sandbox Code Playgroud)

  • 我遇到了这个问题,但当我改变我的行以使用`asset-url`时,我得到'无效的属性值'.知道为什么吗? (2认同)