Rails - Heroku images not finding right path

倾然丶 夕夏残阳落幕 提交于 2019-12-24 02:12:04

问题


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

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