Rail应用程序中的静态图像资产不会在Heroku制作中显示 [英] Static image assets in rails app not displaying on Heroku production
问题描述
我试图在部署之前使用以下命令预编译我的映像:
$ RAILS_ENV =生产bin / rake资产:预编译
$ b
我在开发中使用的模板中使用 image_tag
。
更新: config.serve_static_files
在dev中被称为默认为true,但在生产中被关闭,因为资产应该是通过您的网络服务器提供。我现在只是在heroku上使用免费套餐,并且仍在运行webrick,所以我将此设置为true,但没有运气。
更新2:当我设置 config.serve_static_files
到 false
,heroku没有看到我的任何资产,我的所有样式都消失了,图像仍然不明显。虽然heroku确实发送了一条警告消息,表示所有的 config.serve_static_files
都能够启用公用文件夹中的所有内容,并与资产管道无关。我想这与资产管道无关,因为它只是提供公用文件夹中的资产,并没有查看我们的资产目录。我还看到,heroku在部署时运行预编译命令,所以我不需要每次都这样做。
这让我想知道使用 image_tag
调用我的资产的方式可能是问题所在,但我我不知道为什么会这样?
更新3:资产管道的导轨指南说:在常规视图中,您可以访问public / assets / images目录中的图像像这样:<%= image_tagrails.png%>
。我使用这个约定调用我的图像,如<% = image_tag(lab49,size:80x30)%>
,但图片仍然不会出现。更新4:看到我的答案。
如果您来到这篇文章,并且您正在使用Heroku,Heroku只会在您使用
这是可行的:
< %= image_taglab49.png,size:80x30%>
这不起作用(尽管它会在本地):
<%= image_taglab49,size: 80x30%>
I tried pre-compiling my images before deployment using the command:
$ RAILS_ENV=production bin/rake assets:precompile
I am using image_tag
's in my templates that work in development.
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.
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.
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?
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.
Update 4: See my answer.
If you came to this post and you are using Heroku, Heroku will accept your images only if you use the image file extension.
This will work:
<%= image_tag "lab49.png", size: "80x30" %>
This will not work (although it will locally):
<%= image_tag "lab49", size: "80x30" %>
这篇关于Rail应用程序中的静态图像资产不会在Heroku制作中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!