Background image not working in rails style.css on production

2k views Asked by At

Background image style works fine on local as there is no digest on locale, but on production, after assets:precompile, all assets add fingerprints and there is no non-digested asset names in Rails 4. Due to this,

background-image: url('abc.png')

does not work.

Is there any way to let background images work after assets:precompile?

3

There are 3 answers

3
mrzasa On

In CSS use

background-image: url(<%= asset_path 'abc.png' %>)

https://guides.rubyonrails.org/asset_pipeline.html#css-and-erb

and in Sass

image-url("abc.png") 
image-path("abc.png")

https://guides.rubyonrails.org/asset_pipeline.html#css-and-sass

0
Kalpesh Fulpagare On

Try this. The file extension should be scss, will not work in css file.

background-image: url(asset_path('abc.png'));
0
daxtersky On

I'd make sure that your filename has not spaces. This was the issue in my case.