There's a strange issue with Rails 4 on Heroku. When images are compiled they have hashes added to them, yet the reference to those files from within CSS don't have the proper name adjusted. Here's what I mean. I have a file called logo.png. Yet when it shows up on heroku it is viewed as:
/assets/logo-200a00a193ed5e297bb09ddd96afb953.png
However the CSS still states:
background-image:url("./logo.png");
The result: the image doesn't display. Anybody run into this? How can this be resolved?
The hash is because the asset pipeline and server Optimize caching http://guides.rubyonrails.org/asset_pipeline.html
Try something like this:
Goodluck
Sprockets together with Sass has some nifty helpers you can use to get the job done. Sprockets will only process these helpers if your stylesheet file extensions are either
.css.scss
or.css.sass
.Image specific helper:
Agnostic helper:
Or if you want to embed the image data in the css file:
Referencing the Rails documents we see that there are a few ways to link to images from css. Just go to section 2.3.2.
First, make sure your css file has the .scss extension if it's a sass file.
Next, you can use the ruby method, which is really ugly:
Or you can use the specific form that is nicer:
Lastly, you can use the general form:
You can add to your css .erb extension. Ej: style.css.erb
Then you can put:
Only this snippet does not work for me:
But rename stylename.scss to stylename.css.scss helps me.
In Rails 4, you can reference an image located in
assets/images/
in your.SCSS
files easily like this:When you launch the application in development mode (
localhost:3000
), you should see something like:In production mode, your assets will have the cache helper numbers: