Rails 4的图像网址+减少不包括哈希 [英] Image urls for Rails 4 + Less not including the hash
问题描述
我有一个Rails 4应用程序,并使用 less-rails
gem。在 main.css.less
中,我使用以下方式向网站添加背景图片:
body {
background-image:image-url(background.jpg);
}
一切都可以在本地运行,但是当我推送到我的服务器与预编译资产,图像网址未更新以包括散列。也就是说,映像现在生活在 public / assets / background-074c0b767cd8cfb2da93b37ea3596326.jpg
但我的css文件仍然说 url(/assets/background.jpg )
。
下面的链接看起来很有希望,但是没有一个推荐的解决方案有效c> sass ,虽然我尝试过 image-url
, asset-url
-path
')。
.htmlrel =nofollow> asset fingerprinting ,并且是Rails 资产管道
的标准功能,这意味着如果一切都完成,它应该工作:)
我们以前已经有过这方面的经验,而且有90%的经验。
我会推荐2个修正:
1。在您的LESS中使用 asset_url
:
body {
background-image:asset_url(background.jpg);
}
2。 预编辑资产
#config / environments / production.rb
config.serve_static_assets = true
#cmd
rake assets:precompile RAILS_ENV = production
这将使你的资产是静态的,因此允许你的CSS加载它们没有问题! / p>
I have a Rails 4 application and am using the less-rails
gem. In main.css.less
, I add a background image to the site using:
body {
background-image: image-url("background.jpg");
}
Everything works fine locally, but when I push up to my server with precompiled assets, the image url is not updating to include the hash. That is, the image now lives at public/assets/background-074c0b767cd8cfb2da93b37ea3596326.jpg
but my css file still says url(/assets/background.jpg)
.
The link below seemed very promising, but none of the recommended solutions have worked (most of them are specific to sass
, although I did try image-url
, asset-url
and their respective -path
's).
How to reference images in CSS within Rails 4
Any ideas?
The "hash" is known as asset fingerprinting, and is a standard feature of the Rails asset pipeline
, which means it should work if everything is done right :)
We've had experience of this before, and you're 90% of the way there with it
I'd recommend 2 fixes:
1. Use asset_url
in your LESS:
body {
background-image: asset_url("background.jpg");
}
#config/environments/production.rb
config.serve_static_assets = true
#cmd
rake assets:precompile RAILS_ENV=production
This should make your assets static, thus allowing your CSS to load them with no issues!
这篇关于Rails 4的图像网址+减少不包括哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!