rails: assets:precompile doesn't replace image asset paths in CSS

This is with Rails3.1rc and beta1 If I have a background url specified in application.css like so:

body { 
  background: #00ff00 url('rails.png') no-repeat fixed center;
}

The new asset pipeline stuff finds that image in the assets folder. However when I rake assets:compile the resulting compiled CSS still references rails.png but the asset is now called rails-9c0a079bdd7701d7e729bd956823d153.png so my production server wouldn’t be able to serve that asset from the web server.

I would have expected it to expand it to:

url('/assets/rails-9c0a079bdd7701d7e729bd956823d153.png')

About this issue

  • Original URL
  • State: closed
  • Created 13 years ago
  • Comments: 44 (26 by maintainers)

Commits related to this issue

Most upvoted comments

You have to use ERB inside your CSS to fix this. You can do this by appending .erb to your css/scss file:

applications.css.scss.erb

Then you have the asset_path helper available:

body { 
    background: #00ff00 url(<%= asset_path 'rails.png' %>) no-repeat fixed center;
}

Since I was using sass-rails like @kulbida I finally ended up adding the file extension .scss to the offending .css file(s) so they all end in .css.scss, then replaced all instances of url('blah.png') with url(asset-path('blah.png')) (in my case all the blah.pngs were in a /vendored folder).