Rails图像和资产未正确加载 [英] Rails images and assets not being loaded properly
问题描述
例如,在我的Rails应用程序中我有类似的:
For example, in my Rails application I have something like:
.wax_seal {
background: url("wax-seal-small.png");
display: block;
height: 100px;
margin: 0 auto;
width: 92px;
}
.wax_seal:active {
background: url('wax-seal-small-broken.png');
}
在我的 config / environments / production.rb
文件:
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = true
我手动调用资产编译:
bundle exec rake assets:precompile
而且文件是用名字后面的哈希值创建的:
And the files are created with hashes at the end of the name:
wax-seal-small-Uuhqwduhqwdoi234983jewf.png
/ p>
So this doesn't work:
background: url("wax-seal-small.png");
但这很正常(当我手动在Chrome中键入):
But this works fine (when I manually type it in Chrome):
background: url("wax-seal-small-Uuhqwduhqwdoi234983jewf.png");
我在这里缺少什么步骤?
What step am I missing here? How can I make my CSS rules add in that little hash?
添加 config.assets.compile = true
config / environments / production.rb
使它工作,但我在Rails指南中读到这是一个糟糕的做法,由于重大的性能命中。
Adding config.assets.compile = true
in config/environments/production.rb
makes it work, but I read in the Rails guide that it's a bad practice due to significant performance hits.
推荐答案
我在edgerails文档中找到了这个: http://edgeguides.rubyonrails.org/asset_pipeline.html#css-and-assass
I found this in edgerails documentation: http://edgeguides.rubyonrails.org/asset_pipeline.html#css-and-sass
2.3.2 CSS和Sass
2.3.2 CSS and Sass
当使用资产管道时,必须重写资产路径,并且sass-rails提供-url和-path帮助程序(在Sass中用连字符表示,对于以下资源类别:图片,字体,视频,音频,JavaScript和样式表。
When using the asset pipeline, paths to assets must be re-written and sass-rails provides -url and -path helpers (hyphenated in Sass, underscored in Ruby) for the following asset classes: image, font, video, audio, JavaScript and stylesheet.
-
image-url rails.png)变成url(/assets/rails.png)
-
成为/assets/rails.png
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) (/assets/rails.png)
-
asset-path(rails.png,image)变成/ assets / rails .png
asset-url("rails.png", image) becomes url(/assets/rails.png)
asset-path("rails.png", image) becomes "/assets/rails.png"
这篇关于Rails图像和资产未正确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!