简体   繁体   English

Heroku打开时未加载背景图片

[英]Background Image not loading on heroku open

When deploying my app on heroku my background image is not showing up.. 在heroku上部署我的应用程序时,我的背景图片没有显示。

My Logo image does show up though. 我的徽标图像确实显示了。

They are both in my assets/images file 它们都在我的资产/图像文件中

My background is referenced in my assets/stylesheets/application.css.scss file: 我的资产/样式表/application.css.scss文件中引用了我的背景:

body {
    margin:0px;
    background: url('city-lights1.jpg');
}

And my Logo is referenced in my views/layouts/pages/home file: 在我的views / layouts / pages / home文件中引用了我的徽标:

<div class='log-image'>
        <%= image_tag("logo.png", alt: "image", class:'img-responsive logo') %>
</div>

Why does my background image not load but my logo image does? 为什么我的背景图片无法加载,但徽标图像却加载?

I'm using twitter bootstrap 我正在使用Twitter Bootstrap

You're not using the Rails helpers in your application.scss as you did in your home page, so the url of the background image is not referencing the image as expected. 您没有像在主页中那样在application.scss中使用Rails帮助器,因此背景图像的url没有按预期引用该图像。

You should use the rails 4 helper in your application.scss file: 您应该在application.scss文件中使用rails 4 helper:

background-image: asset-url("city-lights1.jpg");
/* Or */
background-image: image-url("city-lights1.jpg");

Also, you can reference the asset directly (Note that you don't need to include the /image folder in the url). 另外,您可以直接引用资产(请注意,您不需要在URL中包含/ image文件夹)。 Keep in mind that this url reference is not going to work in Heroku if you precompile your assets: 请记住,如果您预编译资产,则此URL引用在Heroku中将不起作用:

background-image: url("/assets/city-lights1.jpg");

There's more detailed information about how to reference images within the assets pipeline on the Rails' Official documentation . Rails的官方文档中,有关于如何在资产管道中引用图像的更多详细信息。

In addition to the previous solution, you will need this: 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

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

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