March 24, 2016

How to reference rails assets in your css?

Ruby on rails uses the asset pipeline for the assets in your Rails application. In order to use these assets in your styles you need to two things. First of all you need to make sure that you are using SASS, and assuming you have the default setup you can do this by changing your .css files to .scss

Once you have done that you are ready to use asset-path in your styles, such as

.some-div {
  background: url(asset-path('some_image.png'));
}

Now when you compile your assets on deployment the asset pipeline will reference the correct file as desired in production.