Static image assets in rails app not displaying on Heroku production

你离开我真会死。 提交于 2020-01-14 13:39:09

问题


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.


回答1:


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" %>



回答2:


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.



来源:https://stackoverflow.com/questions/35320418/static-image-assets-in-rails-app-not-displaying-on-heroku-production

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!