Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static image assets in rails app not displaying on Heroku production

I tried pre-compiling my images before deployment using the command:

$ RAILS_ENV=production bin/rake assets:precompile

I am using image_tag's in my templates that work in development.

Update: config.serve_static_files is said to default to true in dev, but then is turned off in production because the assets should be provided via your web server. I currently am just using the free tier on heroku and am still running webrick, so I have this set to true, but no luck.

Update 2: When I set config.serve_static_files to false, heroku does not see any of my assets, all my stylings go away and images remain unfound. Although heroku does send a warning message upon pushing to master saying that all "config.serve_static_files does is enables serving everything in the public folder and is unrelated to the asset pipeline.". I suppose it is unrelated to the asset pipeline in that it is just serving up the assets in the public folder and does not look at our assets directory. I also see that heroku runs the precompile command upon deployment, so I don't need to do that each time.

This makes me wonder if the way I am calling my assets using the image_tag could be the problem, but I am not sure why that would be?

Update 3: The rails guide for the asset pipeline says "In regular views you can access images in the public/assets/images directory like this: <%= image_tag "rails.png" %>. I am calling for my image using this convention like so <%= image_tag("lab49", size: "80x30") %>, but the image will still not appear.

Update 4: See my answer.

like image 306
Jbur43 Avatar asked Dec 10 '22 18:12

Jbur43


2 Answers

If you came to this post and you are using Heroku, Heroku will accept your images only if you use the image file extension.

This will work:

<%= image_tag "lab49.png", size: "80x30" %>

This will not work (although it will locally):

<%= image_tag "lab49", size: "80x30" %>
like image 112
Jbur43 Avatar answered Jan 19 '23 00:01

Jbur43


Something to watch out for: I had jpeg files in my assets/images folder. When I ran rake assets:precompile they were turned into .jpg files. After renaming them in my assets/images folder to .jpg, precompiling, and pushing again they displayed on Heroku just fine.

like image 28
Peter Tascio Avatar answered Jan 18 '23 23:01

Peter Tascio