Rails Assets are AWOL in Production
Written on November 2, 2014
I'm writing a book about building and deploying web applications with Haskell and Yesod. Want to know when it's released? Click here.
Fixing the images were a slightly different issue. I noticed that in production, Rails’ ‘Asset Pipeline’ adds cache-busters to all of the images, which is the right thing to do. It doesn’t use query strings — which was once The Right Way™ but is now known to be The Old Way™ — so it’s following best practice there too.
The problem is, all the image paths have to be rewritten in some compile step to match the new cache-busting filenames. Rails is not smart enough to recognise a relative asset path in a stylesheet and rewrite it. Instead, you have to change your native, portable style rules into a Rails-specific bastardisation.
// Normal style rule. Works everywhere. .normal_element background: url(unicorn.png) // Rails-specific. Also won’t work without quotes. .rails_element background: asset_url('unicorn.png')
This is a minor pain practically, and a wisdom-tooth philosophically. It’s one example of Rails tightly coupling your otherwise agnostic code to the framework.