ruby-on-rails-5asset-pipelinesprockets

Rails 5.2 Asset Pipeline images not loading on heroku


I am working with a Rails 5.2 application

Locally I added a new image to app/assets/images/my-file-name.jpg

and then in my .erb file I reference it using

<%= image_tag "my-file-name.jpg" %>

Then when I deploy to Heroku I run the following steps locally

  1. rake assets:clobber <!-- this destroys the public/assets folder as expected
  2. RAILS_ENV=production bundle exec rake assets:precompile this creates a new public/assets folder but now each of the files within the assets folder have a hash after their name. So my file my-file-name.jpg has become my-file-name-{BigLongCacheBustingHashHere}.jpg
  3. git push staging <-- Deploy to my staging environment and check that the log says assets compiling -- all good.
  4. Check staging environment if the image is being served and I notice that the app cannot find my image because my <%= image_tag my-file-name.jpg %> is still producing a static html link WITHOUT the hash. It looks like this <img src="my-file-name.jpg"> when I believe it should be producing a tag that looks like <img src="my-file-name-{BigLongCashBustingHashHere}.jpg"

Why do you think the <%= image_tag %> is not producing production friendly urls? I can see that it is working elsewhere in the app so not sure where I am going wrong.


Solution

  • OMG I lost hours of my life on this. Rails assets does not like .jpeg extensions and will flat out ignore them in production. So I just changed the file name to my-file-name.jpg and now Rails is happy