65

I have spent the better part of the day trying to get images to load on my heroku app. Everything I try works locally, but not after being deployed to heroku.

I have png files saved in the images folder under my assets. I am referencing these images with syntax in my css such as;

#signin {
  background: url(<%= asset_path 'sf.png' %>);
  background-size: 100%;
}

In heroku when I inspect the background the assets/sf.png link is there but when you click it it shows a broken image, suggesting it did not load properly.

I've tried toggling config.serve_static_assets = false in the production.rb file between true and false and neither works.

I also have

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

Precompile is always successful.

Rails 4. Any ideas on what else to try?

4

9 回答 9

93

我需要结合几种解决方案来完成这项工作,这就是我所做的:

宝石文件

gem 'rails_12factor', group: :production

在我的Heroku 控制台中

heroku labs:enable user-env-compile -a yourapp

生产.rb

config.serve_static_assets = true
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
config.assets.compile = true

我不需要在本地预编译资产。

于 2013-08-25T11:15:47.597 回答
57

你需要做两件事来解决它。production.rb首先,在文件中将这两行从 false 更改为 true 。

      config.assets.compile = true
      config.assets.digest = true

其次,如果你的图像有这样的语法

    background: url("imgo.jpg") 

将其更改为

     background: image-url("image.jpg")

我希望它能完成你的工作。

于 2015-02-03T11:09:48.787 回答
27

我遇到的另一个问题是,在将资产加载到heroku之前,我在本地预编译了我的资产。这需要您遵循一组不同的步骤,如下所示。如果您在本地预编译资产,则必须遵循这些步骤,否则您对资产文件夹所做的任何更新都不会反映在 prod.xml 中。

https://devcenter.heroku.com/articles/rails-asset-pipeline

RAILS_ENV=production bundle exec rake assets:precompile

commitpush服务器。

于 2013-09-20T15:06:19.080 回答
13

我有一个类似的问题,我用 custom.css.scss 中的以下行解决了它。告诉我这是否适合你。

background: image-url('sf.png')

根据您使用的是 ERB 还是 Sass,以不同的方式引用资产,请参阅 Ruby on Rails 指南

于 2014-02-01T17:14:50.550 回答
3

我还没有评论的声誉,但重要的是要注意 Heroku 实验室功能已被删除,所以现在您将收到“没有这样的功能:user-env-compile”错误

更多:https ://github.com/Crowdtilt/CrowdtiltOpen/issues/251

于 2014-11-07T17:25:48.347 回答
2

Rails ('4.1.5') 我有一个类似的问题,图像没有在 Heroku 上显示,但在本地显示。我没有使用回形针或carrierwave gem,我在本地预编译并使用RAILS_ENV=production我推送到 github,它可以很好地部署到 Heroku。

我通过以下方式解决了这个问题:

config.serve_static_assets = true
config.assets.compile = true
config.assets.js_compressor = :uglifier
config.assets.digest = true

// delete precompiled assets
bundle exec rake assets:clobber --trace
RAIL_ENV=production bundle exec rake assets:clobber --trace

将图像从 app/assets 复制到 public/assets。然后:

// tests should pass
bundle exec rake assets:precompile --trace
RAILS_ENV=production bundle exec rake assets:precompile --trace

git commit
git push

它在 Heroku 上运行良好。

于 2015-11-02T13:38:31.860 回答
1

我在只显示图像时遇到了类似的问题。作为 Rails 新手,我不知道我可以使用:

<%= image_tag("fileName.png", alt: "File Name Fancy", size: "100x100")%>

而不是传统的html。

image_tag 在 rails api 中进行了解释,但我发现它的使用在这里得到了更好的解释:http: //apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag

我添加到我的应用程序中的只是这个宝石: gem 'rails_12factor', group: :production

如 heroku 资产管道文档中所述。https://devcenter.heroku.com/articles/rails-4-asset-pipeline

于 2015-04-12T01:04:24.170 回答
1

我也尝试了许多解决方案,但我找到了一个非常宝贵的解决方案和解释 1. Heroku 在公共文件夹中查找资产,这意味着您必须预编译您的资产,但如果您像我一样正在寻找一种方法来预编译我的资产我的开发环境设置为 gem sqlite 和生产设置为 pg 然后你会这样做。

在你的 production.rb

config.serve_static_assets = true

如果您没有安装 gem pg,您需要将其注释掉并更改您的生产环境以使用 gem sqlite 并运行它

RAILS_ENV=production bundle exec rake assets:precompile

当所有资产都已预编译后,切换回默认设置并 git add .,commit,然后推送到 heroku

于 2016-05-09T22:03:04.447 回答
-1

默认情况下,图像不会作为资产提供,只有 css 和 js。你应该看看答案

Syed Ehtsham Abbas 在这个问题中 Heroku 不会在 Rails 4 中的资产管道下编译文件

于 2013-08-19T22:28:35.737 回答