1

When assets are compiled, Sprockets digests each file's path and adds a hash to it, so for example, this:

example.com/assets/Image.png

is turned into this:

example.com/assets/Image-f8hs838fa1479fhsu1341d483nj4b9fa95f850h3n47d74a7cd81a22ljs8f3c3.png

To access an image or other file in Rails, one simply has to call its path, like this: <%= image_tag 'Image.png' %>.

But what if you're in a position when you can't use erb? For example, in a CSS file, setting a background-image using a file's undigested path doesn't work, because the path doesn't actually exist on production, so something like background-image: url('../images/Image.png') or background-image: url('/assets/Image.png') lead to a missing image. Another example is writing an image into a text block. Trying to print this:

<% post.text = "text <img src='/assets/Image.png'/> more text" %>
<%= post.text.html_safe %>

will once again result in a missing image on production.

Is there a way to access files without the hash? Or is there a way to call the Rails method in situations like the two examples I gave? Assuming no concern for security.

Joe Morano
  • 1,715
  • 10
  • 50
  • 114
  • 1
    Oddly I can access my assets files directly on Heroku, need to go thoroughly into asset pipeline to know why.. One alternative as described here http://guides.rubyonrails.org/asset_pipeline.html#css-and-erb is to make your CSS file an CSS.ERB file – Maxence Jul 22 '18 at 00:49
  • @Maxence Oh weird, I use Heroku too. Did you do any custom configuration? – Joe Morano Jul 22 '18 at 00:51
  • 1
    Actually I am not yet sure if this is a good thing or a terrrible mistake I did. The asset pipeline is rather complex and very different between dev and prod. I prefer to let more experienced users with asset pipeline reply to you. ( And I don't know straight away what provokes this but obviously this can be done) – Maxence Jul 22 '18 at 00:52
  • @Maxence Interesting. Fair enough though. – Joe Morano Jul 22 '18 at 01:01
  • https://stackoverflow.com/questions/7433558/use-rails-3-1-asset-paths-in-a-scss-partial https://stackoverflow.com/questions/16843143/rails-4-image-path-image-url-and-asset-url-no-longer-work-in-scss-files – Brad Werth Jul 22 '18 at 01:15

2 Answers2

0

In rails css you should use image-url helper instead of url:

background: image-url('image.jpg');

In rails view you should use image_url or image_path helper, like this:

"<div style='background: url(#{image_url('image.jpg')})'></div>".html_safe
Bogdan
  • 199
  • 4
0

The reason why digest hash is there is to permit caching and cache busting when image contents changes. Therefore most applications will want to use the path with the digest hash in it.

In CSS (generated without the assistance of Rails helpers, for instance) this is rather easy to do with regular expressions. It is also fairly straightforward to do global search and replace of non-digested URLs or paths with digested URLs or paths.

An alternate approach is to copy digested paths to non-digested paths in a post-build step, but this is suboptimal because caching won't work correctly.

D. SM
  • 13,584
  • 3
  • 12
  • 21