我有一个欢迎 Controller 和一个 welcome.css.scss 布局。在欢迎布局中有这段代码:
body {
background-image: url("/assets/images/sampleimage.jpeg");
background-repeat: no-repeat;
}
在 assets/images 中,我有一张名为 sampleimage.jpeg 的图片,我想将其作为欢迎索引 View 的背景。
我拥有以上所有内容,但我既没有收到背景图片,也没有收到错误消息。感谢您的帮助。
最佳答案
既然你正在使用 SCSS,让我给你一些想法......
--
Assets 路径
首先,你需要的是asset_path
helpers ,允许 Rails 调用 Assets ,无论它们是在标准的“ Assets 管道”中,还是在预编译的“静态” Assets 区域中
您遇到的直接问题是您在 background-image
属性上调用了一个裸体 url
。这不会调用您需要的文件,因为路径是相对的,因此无法调用(尝试从浏览器访问 /assets
)
实现方式如下:
#app/assets/stylesheets/welcome.css.scss
body {
background: {
image: asset-url("sampleimage.jpeg");
repeat: no-repeat;
}
}
asset-url
路径是可能的,因为 Rails Assets 管道的 SCSS/SASS 元素 preprocesses您的 Assets 请求
关于css - 我的 Controller 索引 View 的背景图片未加载到 Rails 4 应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25594377/