Search code examples
cssruby-on-railsprecompile

Rails 4.1 image url's still calling uncompiled assets


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)


Solution

  • 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