Heroku不提供背景图片,localhost呢?

Ale*_*lex 11 css ruby-on-rails heroku asset-pipeline ruby-on-rails-4

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

行为很奇怪:我的localhost正确显示背景图片,Heroku没有.

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

ActionController::RoutingError (No route matches [GET] "/assets/piano.jpg"):
Run Code Online (Sandbox Code Playgroud)

我通过在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;
}
Run Code Online (Sandbox Code Playgroud)

我用以下代码触发了这个,我在静态页面上有这个代码:

<body class="full">
....
</body>
Run Code Online (Sandbox Code Playgroud)

我已经在生产中运行了宝石:

group :production do
  gem 'pg'
  gem 'rails_12factor'
end
Run Code Online (Sandbox Code Playgroud)

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

config.serve_static_assets = true
Run Code Online (Sandbox Code Playgroud)

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

小智 25

确保在production.rb文件中设置它们

config.cache_classes = true
config.serve_static_assets = true
config.assets.compile = true
config.assets.digest = true
Run Code Online (Sandbox Code Playgroud)


Ale*_*lex 12

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

RAILS_ENV=production bundle exec rake assets:precompile
Run Code Online (Sandbox Code Playgroud)

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

以前我只是试图运行:

rake assets:precompile
Run Code Online (Sandbox Code Playgroud)

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

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