11

我的 Rails 应用程序有问题(Rails 4.0.0.rc2,ruby 2.0.0p195)。

行为很奇怪:我的本地主机正确显示了背景图片,而 Heroku 没有。

在 heroku 日志中,我可以看到以下错误:

ActionController::RoutingError (No route matches [GET] "/assets/piano.jpg"):

我通过在我的 custom.css.scss 中插入以下代码来创建背景图像:

.full { 
  background: image-url("piano.jpg") no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

我使用静态页面上的以下代码触发此操作:

<body class="full">
....
</body>

我已经在生产中运行了 gem:

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

在 production.rb 中,我将以下设置设置为 true:

config.serve_static_assets = true

但是,图像未显示。你能帮我吗?

4

5 回答 5

25

确保在您的 production.rb 文件中设置这些

config.cache_classes = true
config.serve_static_assets = true
config.assets.compile = true
config.assets.digest = true
于 2013-10-05T17:59:03.477 回答
12

我自己找到了该问题的解决方案:

RAILS_ENV=production bundle exec rake assets:precompile

在我的控制台中运行此命令后,图片正确显示。

以前我只尝试过运行:

rake assets:precompile

仅此一点没有帮助。您必须在命令中处理生产环境。

我希望这可以作为其他用户的参考。

于 2013-10-05T21:16:46.463 回答
4

我需要结合其他答案来为我工作。

我需要使用 @Brock90 的生产配置设置,并像 Alex 提到的那样预编译资产。

于 2015-01-20T02:56:50.123 回答
0

使用background属性和 SASS

background: image-url("my_image.png") no-repeat scroll 0 0 rgba(0, 0, 0, 0);
于 2014-11-27T17:24:06.033 回答
-2

不要将图片放在 images/ 目录的子目录中

如果有人仍然有这个问题。我到处寻找解决方案,我想我什么都试过了。就我而言,我有类似的情况

background-image: url("containers/filled/Firkin-Keg-5-Gallons_filled.png");

所以我在图像中有文件夹。这适用于 localhost,但不适用于 heroku。

background-image: url("Firkin-Keg-5-Gallons_filled.png");

没有更多的子目录。

编辑 这是错误的。请参阅下面的评论以正确使用

于 2017-06-10T21:22:29.567 回答