简体   繁体   中英

Images not loading on rails heroku app

For some reason when I tried to update my websites logo the image does not appear when I push it to Heroku, just the images' file name as a link. The images appear fine locally. I have png files saved in the images folder under my assets, so its not an issue of Heroku not finding it. I saw a post similar to this, but it really didn't answer my question. So why is the new image not appearing?

This is my experience: [ ruby 2.30, rails 4.2.6 ]

following heroku instruction asset pipeline on rails 4

in config/application.rb add setting below

config.serve_static_assets = true  # deprecated

use instead

config.serve_static_files = true   # Ok


in Gemfile add

gem 'rails_12factor', group: :production

then, as heroku says, set ruby version in your Gemfile

gem ruby '2.3.0'


Now you can update your dependencies running

bundle install


Then you should adapt your source code :

changing image_tag "source_name" everywhere it appears without extension

<link_to image_tag("icon") ... %>

with image_tag "source_name" with extensions (.png, .jpg or whatever)

<link_to image_tag("icon.png") ...%>

You shouldn't run
RAILS_ENV=production rake assets:precompile
because heroku does this for you during deploy.

... and now is time to deploy

git add <files modified>
git commit -m "comments your commit"
git push heroku <your branch>

This solved for me ... I hope also for you.

Add this to show precompiled image. On heroku default mode is production.So it need to display precompiled images. When an image or assets are precompiled. It compresses the code and image gets renamed to for example:(image_42342j3n42b3n44234234.jpg) so you need to show this renamed image. Thus you need to add

config.assets.digest = true

this to your production.rb

If you display images like

<img src='src_of_your_image'>

This will not work, because of asset pipeline , you have to use image_path helper

<%= image_path 'src_of_your_image' %>

Image path helper will properly render you image even in production mode, after your images get their path distorted with custom hash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM