Reputation: 10673
Using Rails, I'm fine with the image tag helper in an HTML file but how do I include images in a CSS file. I've tried replacing the url from:
background:url('/assets/img/banners/img3.jpg')
to...
background:url(<%= image_tag "banners/img3.jpg" %>)
but this doesn't work. Do I need to change the extension of my css files to make it work?
Upvotes: 1
Views: 1473
Reputation: 34135
Yes, your css should have erb
extension at the end. example: style.css.erb
. then you can do this
.class { background-image: url(<%= asset_path 'image.png' %>) }
this will work because asset pipeline automatically evaluates ERB due to the erb
file extension.n this example, it would make sense to have an image in one of the asset load paths, such as app/assets/images/image.png
, which would be referenced here. If this image is already available in public/assets
as a fingerprinted file, then that path is referenced.
Read more at The Asset Pipeline Guide
Upvotes: 1
Reputation: 21262
You don't have any helpers in CSS, and that's OK; you can specify the path manually and it will always work.
Also remember that paths are relative to the stylesheet itself, not to the HTML document.
Upvotes: 1