问题
I'm using Rails 5.2.0 and have deployed to Heroku. I have images being loaded using the following call:
...link_to image_tag("/assets/avatar.png", class: "img-responsive")...
on my local machine and they load properly.
The directory structure is:
my_project
app
| assets
| | images
| | | avatar.png
However, on Heroku, the images aren't loading. I have checked other posts but am not sure if I need to modify my Gemfile / production.rb etc. I have also tried changing the path on Heroku to
...link_to image_tag("app/assets/avatar.png", class: "img-responsive")...
adding the /app
to the path but this hasn't worked either.
My question is do I need to just change the path so Heroku can find the images. If so, what does the path need to be? Where does Heroku "start" the path from when a Rails app is running?
Or is there something else that needs to be done to allow Heroku to find and load these images?
回答1:
It will be like this below
<%= link_to image_tag("avatar.png", class: "img-responsive"), url_path %>
Look the image_tag for how image_tag works.
Update production.rb
file config.assets.compile
false
to true
# config/environments/production.rb
...
config.assets.compile = true
...
来源:https://stackoverflow.com/questions/50716681/rails-heroku-images-not-finding-right-path