简体   繁体   English

Rails不显示背景图片

[英]Rails doesn't show background-image

I have custom CSS file style.css, not in application.css: 我有自定义CSS文件style.css,而不是application.css:

background-image: url('book.png');

I did 我做了

rake assets:precompile RAILS_ENV=production

but it doesn't show image. 但它不显示图像。 Also I gave permissions to image(755). 我也给了image(755)权限。 How can I solve it? 我该如何解决?

And there is no errors in console 而且控制台中没有错误

log/production.log 登录/ production.log

I, [2014-12-24T01:22:04.323979 #27157]  INFO -- : Started GET "/login" for 81.21.82.67 at 2014-12-24 01:22:04 -0500
I, [2014-12-24T01:22:04.329702 #27157]  INFO -- : Processing by BookController#login as HTML
I, [2014-12-24T01:22:04.333589 #27157]  INFO -- :   Rendered book/login.html.erb within layouts/application (1.0ms)
I, [2014-12-24T01:22:04.334700 #27157]  INFO -- : Completed 200 OK in 5ms (Views: 2.9ms | ActiveRecord: 0.0ms)

When I open from browser http://example.com/assets/book.jpg it doesn't open, too 当我从浏览器http://example.com/assets/book.jpg打开时,它也没有打开

After precompile image path will be changed so you need to use asset_path for get correct image path from asset. 预编译后,图像路径将被更改,因此您需要使用asset_path从资产获取正确的图像路径。

If you use asset_path then it will be easy to get image after precompile. 如果使用asset_path,则在预编译后很容易获得图像。

Use this: 用这个:

background-image: asset_path('book.png');

instead of: 代替:

background-image: url('book.png');

Assuming that the book.png is a local image, there are probably two changes you need to make: 假设book.png是本地图像,则可能需要进行两项更改:

a. 一种。 Change style.css to style.css.scss 更改style.cssstyle.css.scss

b. Replace the background-image: url('book.png'); 替换background-image: url('book.png'); with background-image: image-url('book.png'); 带有background-image: image-url('book.png');

This ensures that the CSS uses the full asset URL for the book.png image. 这样可以确保CSS使用book.png图片的完整资产URL。

Guides are your friends . 向导是您的朋友

The problem is that assets in the asset pipeline are renamed to include a checksum that reflects their content. 问题在于资产管道中的资产被重命名为包括反映其内容的校验和。 The problem with that is, that the link to the actual image is different than the one you expect. 问题在于,到实际图像的链接与您期望的链接不同。 Only in production, since assets are only precompiled for production environments. 仅在生产中,因为资产仅针对生产环境进行了预编译。 In development there's no problem, because assets are recompiled in-place after any change and served with parameters that minimize possible caching. 在开发过程中没有问题,因为资产在进行任何更改后都将在原地重新编译,并带有最小化可能缓存的参数。

It's for the greater good. 为了更大的利益。 The result of this is that if the file changes in the new release, it gets the different name when served (even if you didn't rename it yourself), so browsers and caching systems detect the change immediately and request the new asset. 这样做的结果是,如果文件在新发行版中发生更改,则在提供服务时它将获得不同的名称(即使您自己未重命名),因此浏览器和缓存系统会立即检测到更改并请求新资产。 No pain with cache invalidation. 不用担心缓存失效。

ERB is used during asset precompilation , and you can use it by adding .erb to file name, like login.css.erb . ERB在资产预编译期间使用 ,您可以通过在文件名中添加.erb来使用它,例如login.css.erb There is an example in the guides . 指南中一个示例

.class {
  background-image: url(<%= asset_path 'image.png' %>)
}

The guides even describe a way of embedding an image right into CSS in base64 . 这些指南甚至描述了一种将图像直接嵌入base64中的 CSS中的方法。 It works well with small images linke PNG icons, one less request and only +33% to image size. 它适用于链接PNG图标的小图像,要求少一个,并且图像大小仅+ 33%。

#logo {
  background: url(<%= asset_data_uri 'logo.png' %>)
}

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

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