简体   繁体   English

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

[英]Heroku does not serve background image, localhost does?

I have a problem with my rails application (Rails 4.0.0.rc2, ruby 2.0.0p195). 我的rails应用程序有问题(Rails 4.0.0.rc2,ruby 2.0.0p195)。

The behavior is weird: My localhost shows the background picture properly, Heroku doesn't. 行为很奇怪:我的localhost正确显示背景图片,Heroku没有。

In the heroku logs I can see the following error: 在heroku日志中,我可以看到以下错误:

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

I have created the background image by inserting the following code in my custom.css.scss: 我通过在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;
}

And I am triggering this with the following code, which I have on my static page: 我用以下代码触发了这个,我在静态页面上有这个代码:

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

I already have the gem running in production: 我已经在生产中运行了宝石:

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

In production.rb I have set the following setting to true: 在production.rb中,我将以下设置设置为true:

config.serve_static_assets = true

However, the image is not being shown. 但是,图像未显示。 Can you help me out? 你能帮我吗?

Make sure you set these in your production.rb file 确保在production.rb文件中设置它们

config.cache_classes = true
config.serve_static_assets = true
config.assets.compile = true
config.assets.digest = true

I have found the solution for the issue myself: 我自己找到了解决问题的方法:

RAILS_ENV=production bundle exec rake assets:precompile

After running this command in my console, the picture was shown properly. 在我的控制台中运行此命令后,图片显示正常。

Previously I had only tried to run: 以前我只是试图运行:

rake assets:precompile

That alone didn't help. 仅此一点没有帮助。 You have to address the production environment in your command. 您必须在命令中处理生产环境。

I hope this serves as a reference for other users. 我希望这可以作为其他用户的参考。

I needed a combination of the other answers to work for me. 我需要结合其他答案为我工作。

I needed to use @Brock90's production config settings as well as precompile the assets as Alex mentioned. 我需要使用@ Brock90的生产配置设置以及像Alex提到的那样预编译资产。

使用background属性和SASS

background: image-url("my_image.png") no-repeat scroll 0 0 rgba(0, 0, 0, 0);

Don't have your pictures in sub-directories within the images/ directory 不要将图片放在images /目录中的子目录中

If anyone is still having trouble with this. 如果有人仍然遇到这个问题。 I was looking everywhere for a solution and I thought I tried everything. 我到处寻找解决方案,我以为我尝试了一切。 For my case I had instances like 对于我的情况,我有像

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

So I had folders within images. 所以我在图像中有文件夹。 This worked fine for localhost, but it would not work on heroku. 这适用于localhost,但它不适用于heroku。

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

no more sub-directories. 没有更多的子目录。

EDIT This is wrong. 编辑这是错误的。 Refer to comments below for proper usage 请参阅以下注释以确保正确使用

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

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