Rails-Heroku图像找不到正确的路径 [英] Rails - Heroku images not finding right path
问题描述
我正在使用Rails 5.2.0,并已部署到Heroku.我有使用以下调用加载的图像:
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.
目录结构为:
my_project
app
| assets
| | images
| | | avatar.png
但是,在Heroku上,图像未加载.我检查了其他帖子,但是不确定是否需要修改我的Gemfile/production.rb等.我也尝试过将Heroku上的路径更改为
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")...
将/app
添加到路径,但这也不起作用.
adding the /app
to the path but this hasn't worked either.
我的问题是我是否只需要更改路径,以便Heroku可以找到图像.如果是这样,那么路径应该是什么? Heroku在运行Rails应用程序时从何处开始"路径?
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?
还是需要做一些其他事情以使Heroku查找并加载这些图像?
Or is there something else that needs to be done to allow Heroku to find and load these images?
推荐答案
下面将是这样
<%= link_to image_tag("avatar.png", class: "img-responsive"), url_path %>
查看 image_tag
关于image_tag的工作原理.
Look the image_tag
for how image_tag works.
将production.rb
文件config.assets.compile
false
更新为true
# config/environments/production.rb
...
config.assets.compile = true
...
这篇关于Rails-Heroku图像找不到正确的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!