【发布时间】:2014-09-04 10:01:53
【问题描述】:
我对样式表文件中的图像有疑问。这是描述:
我正在使用 rails 4.1.1 和 sass 4.0.3,我正在部署 OpenShift。我需要在我的 scss 文件中提供图像 url。我阅读了很多文档,但找不到答案。
我发现了这个:
2.3.2 CSS and Sass
When using the asset pipeline, paths to assets must be re-written and sass-rails provides -url and -path helpers (hyphenated in Sass, underscored in Ruby) for the following asset classes: image, font, video, audio, JavaScript and stylesheet.
image-url("rails.png") becomes url(/assets/rails.png)
image-path("rails.png") becomes "/assets/rails.png".
The more generic form can also be used:
asset-url("rails.png") becomes url(/assets/rails.png)
asset-path("rails.png") becomes "/assets/rails.png"
我在 app/assets/images 中有图片!
所以在我的 scss 文件 (main.css.scss) 中我使用的是这个:
background: url(image-path('clothes-line.jpg'));
在开发模式下一切正常,但在 openshift 的生产环境中,它不会在图像 url 的末尾添加哈希。所以网址是:
background-image: url("/assets/clothes-line.jpg");
但应该是:
background-image: url("/assets/clothes-line-d8c5459295983599e58ddb8f7e6aa010.jpg");
我不知道为什么。我尝试了预编译资产,增加资产版本号,更改一些配置。没有任何帮助。
请你帮帮我。
更新:
好的,这是我正在处理的网络:
http://origi-railsshop.rhcloud.com/
在标题是黑色背景,因为图像丢失。使用一些工具来查看 css,例如 Firebug。
background-image: url("/assets/clothes-line.jpg");
尝试编辑和添加哈希:
background-image: url("/assets/clothes-line-d8c5459295983599e58ddb8f7e6aa010.jpg");
它将加载图像。
已解决(直到 8 小时才能发送答案,所以我把答案放在这里):
所以现在可以了。
遗憾的是,我不确定是什么原因造成的。我使用了配置和 rhc 工具,它在我触发命令后开始工作:
rhc app deploy -a <app_id> --ref <commit_id>
我在 config/enviroments/production.rb 中也有未注释的行:
config.assets.css_compressor = :sass
我也在同一个文件中玩过:
config.assets.compile
但是我在这里找到了关于asset.compile的很好的描述:http://blog.55minutes.com/2012/02/untangling-the-rails-asset-pipeline-part-3-configuration/
然后我将其切换回 false。因为在生产中它应该是假的,否则它会在浏览器发出请求时即时预编译资产。
但是我现在无法重现我的问题,所以我不确定这些操作中的哪一个。
感谢您的回答。
【问题讨论】:
-
为什么不能简单地使用背景:image-url("clothes-line.jpg")?
-
因为预编译后没有名字为clothing-line.jpg的图片。资产管道在最后自动添加指纹。所以在我的公用文件夹中是 assets 文件夹,里面是这样的图像 Clothing-line-d8c5459295983599e58ddb8f7e6aa010.jpg。
-
您的图片在公共文件夹中吗?
-
是的,我在 /public/assets/ 中有预编译的图像。这是该图像的 URL:origi-railsshop.rhcloud.com/assets/…
-
我遇到了同样的问题。 config.assets.compile = true 为我解决了!非常感谢。
标签: ruby-on-rails sass openshift assets