问题
So I have got my rails site up on production for the most part but I realized I was missing a few images. I looked at my precompiled css file and the image url's are calling the images directly like url(/assets/gototop.png)
which it won't find since my image is now something like gototop-6c119f88349ddd550e3efcf5bbefe1ad.png
.
How do I get my css to point to the precompiled image file?
Update: also as stated in the linked file I realize I could use the asset_path method but unfortunately I am using scss and I can't just add .erb to the end of the file...seems rails doesnt like to work with multiple compilers. (I tried)
回答1:
This is a common problem caused by asset fingerprinting (not just a Rails 4.1 issue)
The way to fix it is to use a precompiler in your CSS (Rails comes with SASS). A precompiler basically means you can treat CSS files like erb
-- use certain helpers & variables:
#app/assets/stylesheets/application.css.sass
body
background: asset_url("/layout/gototop.png")
asset_url
(seems it's now called asset-url) basically allows you to call assets by their fingerprinted name
As per our discussion, you also need to do this with rake assets:precompile RAILS_ENV=production
. After firing this command in the console, it worked as expected
回答2:
You have to execute this code via ERB first:
Step 1 RENAME: RAILS_ROOT/app/assets/stylesheets/application.css.sass
to RAILS_ROOT/app/assets/stylesheets/application.css.sass.erb
Step 2 ADD ERB TAGS: change asset path to use erb tags
body
background: <%= asset_url("/layout/gototop.png") %>
to verify it worked, see the filename of gototop.png
, it should be something like gototop-029908d2eb2fd4cb3a2f538c6acbd73d.css
.
if not, then post back with sample app on github, so we can help you debug this.
来源:https://stackoverflow.com/questions/22998738/rails-4-1-image-urls-still-calling-uncompiled-assets