简体   繁体   English

Heroku 上不显示图像

[英]Image does not display on Heroku

I have two images on a static web page in a Ruby on Rails web application.我在 Ruby on Rails Z2567A5EC9705EB7AC2C9DZ84033 的 Ruby 页面上有两张图片。 Both images are in the same place:两张图片都在同一个地方:

/app/assets/images

The image tag for both is generated in the same way, barring some attributes:两者的图像标签以相同的方式生成,除了一些属性:

<%= image_tag("logo.png", alt: "Logo", width: 150) %>

<%= image_tag("image.jpeg", alt: "image", class: "img-responsive") %>

Both work fine on the local server, but when I push to production, only the first image displays.两者都可以在本地服务器上正常工作,但是当我推送到生产环境时,只显示第一张图像。 The source shows the tag for the second image is not being processed correctly:源代码显示第二张图片的标签未正确处理:

<img alt="Logo" width="150" src="/assets/logo-9e9dd9c19ef18b3ef26e23f11d73a8817af6cea78a5d9e1a34691d591a780987.png" />

<img alt="image" class="img-responsive" src="/images/image.jpeg" />

I don't understand why one works and the other doesn't?我不明白为什么一个有效而另一个无效?

In terms of context, the first image is PNG and part of the layout, rendered in application.html.erb.在上下文方面,第一个图像是 PNG 和部分布局,呈现在 application.html.erb 中。 The second is part of the page view yielded into application.html.erb.第二个是生成到 application.html.erb 的页面视图的一部分。 Does that make a difference?这有什么区别吗?

Any pointers in the right direction?任何指向正确方向的指针? Thanks for your help.谢谢你的帮助。

Regards,问候,

Jonathan乔纳森

try below code: 尝试以下代码:

config/environments/production.rb 配置/环境/ production.rb

config.assets.compile = true

then run command: 然后运行命令:

RAILS_ENV=production rake assets:precompile

then push all compiles files and menifest file to heroku. 然后将所有编译文件和清单文件推送到heroku。

Seems like you are not precompiling jpeg . 好像你没有预编译jpeg

Make sure you have this line in config/initializers/assets.rb 确保在config/initializers/assets.rb有这一行

Rails.application.config.assets.precompile += %w( *.jpg *.jpeg some-other.css some-other.js)

The problem really is that your assets are not precompiled to work on production. 问题实际上是您的资产未经过预编译才能用于生产。

For a little explanation and lesson to learn from as a newbie, Rails comes bundled with a task to compile the asset manifests and other files in the pipeline. 对于作为新手学习的一点解释和教训,Rails捆绑了一个任务来编译资产清单和管道中的其他文件。

Compiled assets are written to the location specified in config.assets.prefix . 编译资产将写入config.assets.prefix指定的位置。 By default, this is the /assets directory. 默认情况下,这是/ assets目录。

You can call this task on the server during deployment to create compiled versions of your assets directly on the server. 您可以在部署期间在服务器上调用此任务,以直接在服务器上创建资产的编译版本。

You can run the task below from your terminals for your images or assets to show on production and redeploy to heroic: 您可以从终端运行以下任务,以便在生产时显示图像或资产并重新部署为英雄:

RAILS_ENV=production bin/rails assets:precompile

For more read-up, kindly refer to Rails Documentations on Precompiling Assets 有关更多信息,请参阅有关预编译资产的Rails文档

I hope this helps a little for your to learn. 我希望这对你的学习有所帮助。

The difficulty with the accepted answer is that these days Heroku strongly recommends that you use the Heroku asset compiling process rather than precompiling.接受答案的困难在于,如今 Heroku 强烈建议您使用 Heroku 资产编译过程而不是预编译。

I had a similar problem, in my case the image was mounted with我有一个类似的问题,在我的情况下,图像是用

<%= image_tag 'fig_2b', alt: 'graph' %>

This was loading fine in development but not on Heroku.这在开发中加载良好,但在 Heroku 上加载不正常。 This was because I was not specifying the file extension.这是因为我没有指定文件扩展名。 The asset building process on Heroku appears to be more strict, and requires the correct extension. Heroku 上的资产构建过程似乎更加严格,并且需要正确的扩展。 The solution was to add the relevant extension, which in this case was .png , ie解决方案是添加相关扩展名,在本例中为.png ,即

<%= image_tag 'fig_2b.png', alt: 'graph' %>

I wonder whether the problem with the jpeg file in the question was that it should have had the jpg extension.我想知道问题中 jpeg 文件的问题是否在于它应该具有jpg扩展名。 Perhaps at that point in time, the Heroku asset building process did not recognise jpeg .也许在那个时间点,Heroku 资产构建过程无法识别jpeg

Incidentally, I learnt about compiling on Heroku the hard way.顺便说一句,我了解了如何在 Heroku 上进行编译。 I used to precompile the assets, and I struck a sequence of weird problems.我曾经预编译资产,但遇到了一系列奇怪的问题。 The advice I received from Heroku was to only precompile if you have a very deep understanding of sprockets .我从 Heroku 收到的建议是,只有在您对sprockets有非常深入的了解时才进行预编译。 I stopped experiencing such problems when I let Heroku do the compiling.当我让 Heroku 进行编译时,我不再遇到此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM