Heroku不提供背景图片,localhost呢?
我的rails应用程序有问题(Rails 4.0.0.rc2,ruby 2.0.0p195)。
行为很奇怪:我的localhost正确显示背景图片,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; }
我用以下代码触发了这个,我在静态页面上有这个代码:
....
我已经在生产中运行了gem:
group :production do gem 'pg' gem 'rails_12factor' end
在production.rb中,我将以下设置设置为true:
config.serve_static_assets = true
但是,图像未显示。 你能帮我吗?
确保在production.rb文件中设置它们
config.cache_classes = true config.serve_static_assets = true config.assets.compile = true config.assets.digest = true
我自己找到了解决问题的方法:
RAILS_ENV=production bundle exec rake assets:precompile
在我的控制台中运行此命令后,图片显示正确。
以前我只是试图运行:
rake assets:precompile
仅此一点没有帮助。 您必须在命令中处理生产环境。
我希望这可以作为其他用户的参考。
我需要结合其他答案为我工作。
我需要使用@ Brock90的生产配置设置以及像Alex提到的那样预编译资产。
使用background
属性和SASS :
background: image-url("my_image.png") no-repeat scroll 0 0 rgba(0, 0, 0, 0);
不要将图片放在images /目录中的子目录中
如果有人仍然遇到这个问题。 我到处寻找解决方案,我以为我尝试了一切。 对于我的情况,我有像
background-image: url("containers/filled/Firkin-Keg-5-Gallons_filled.png");
所以我在图像中有文件夹。 这适用于localhost,但它不适用于heroku。
background-image: url("Firkin-Keg-5-Gallons_filled.png");
没有更多的子目录。
编辑这是错误的。 请参阅以下评论以确保正确使用