rails应用程序中的静态图像资产不显示在Heroku产品上

时间:2022-11-10 20:37:34

I tried pre-compiling my images before deployment using the command:

我尝试在部署前预编译映像:

$ RAILS_ENV=production bin/rake assets:precompile

$ RAILS_ENV =生产bin /耙资产:预编译

I am using image_tag's in my templates that work in development.

我在开发的模板中使用image_tag's。

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.

更新:配置。serve_static_files在dev中默认为true,但在生产中关闭,因为应该通过web服务器提供资产。我现在正在使用heroku上的免费层,并且还在运行webrick,所以我把这个设置为true,但是没有运气。

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.

更新2:设置配置时。serve_static_files to false, heroku没有看到我的任何资产,所有样式都消失了,图像仍未找到。尽管heroku在推着大师说“所有配置”时确实发出了警告信息。serve_static_files支持为公共文件夹中的所有内容提供服务,与资产管道无关。我认为它与资产管道无关,因为它只提供公共文件夹中的资产,而不查看我们的资产目录。我还看到heroku在部署时运行预编译命令,所以我不需要每次都这样做。

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?

这让我怀疑是否使用image_tag调用我的资产的方式可能是问题所在,但我不确定为什么会是这样?

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.

更新3:资产管道的rails指南说:“在常规视图中,您可以像这样访问公共/资产/图像目录中的图像:<%= image_tag”rails。png " % >。我使用so <%= image_tag(“lab49”,size:“80x30”)%>调用我的图像,但是图像仍然不会出现。

Update 4: See my answer.

更新4:见我的答案。

2 个解决方案

#1


6  

If you came to this post and you are using Heroku, Heroku will accept your images only if you use the image file extension.

如果你来到这篇文章,你正在使用Heroku, Heroku将只接受你的图片,如果你使用图像文件扩展。

This will work:

这将工作:

<%= image_tag "lab49.png", size: "80x30" %>

This will not work (although it will locally):

这将不起作用(尽管在当地会起作用):

<%= image_tag "lab49", size: "80x30" %>

#2


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.

需要注意的是:我的assets文件夹中有jpeg文件。当我运行rake资产:预编译时,它们变成了.jpg文件。在我的资产/图像文件夹中将它们重命名为。jpg,预编译,然后再将它们显示在Heroku上。

#1


6  

If you came to this post and you are using Heroku, Heroku will accept your images only if you use the image file extension.

如果你来到这篇文章,你正在使用Heroku, Heroku将只接受你的图片,如果你使用图像文件扩展。

This will work:

这将工作:

<%= image_tag "lab49.png", size: "80x30" %>

This will not work (although it will locally):

这将不起作用(尽管在当地会起作用):

<%= image_tag "lab49", size: "80x30" %>

#2


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.

需要注意的是:我的assets文件夹中有jpeg文件。当我运行rake资产:预编译时,它们变成了.jpg文件。在我的资产/图像文件夹中将它们重命名为。jpg,预编译,然后再将它们显示在Heroku上。