SCSS文件中的Rails 3.1资产url似乎未正确引用资产 [英] Rails 3.1 asset urls in SCSS files do not seem to be referencing the assets correctly

查看:90
本文介绍了SCSS文件中的Rails 3.1资产url似乎未正确引用资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从Rails 3.0升级到Rails 3.1.

I just upgraded from Rails 3.0 to Rails 3.1.

我有一个foo.css.scss文件,该文件引用了以下图像(/app/assets/images/foo.png):

I have a foo.css.scss file that references an image (/app/assets/images/foo.png) as follows:

.foo {
  background-image: image-url('foo.png');
}

问题是我的foo.png文件未加载,并且我的日志中看到404错误. 生成的实际css条目是:

The problem is that my foo.png file is not loaded and I see 404 errors in my logs. The actual css entry that is generated is:

background-image: url(/images/foo.png);

这是错误的(?),因为可以在/assets/foo.png而不是/images/foo.png上找到图像.

which is wrong (?) because the image can be found at /assets/foo.png and not at /images/foo.png.

请注意,我仍在development模式下工作.

Note that I am still working on development mode.

另一个重要说明.如果我将foo.css.scss文件重命名为foo.css.erb并使用:

Another important note. If I rename my foo.css.scss file to foo.css.erb and use:

background-image: url(<%= image_path('foo.png') %>);

工作正常,因为它会生成/assets/foo.png.

it works ok, because it generates /assets/foo.png.

所以,问题是为什么我的scss预编译器无法生成正确的css?

So, the question is why my scss precompiler does not generate the correct css?

更新:我的foo.css.scss文件位于:

app/assets/stylesheets/sub_dir/foo.css.scss

有什么区别吗?

推荐答案

我尝试了各种解决方案.最优雅的是:

I have tried various solutions. The most elegant one was the following:

.foo {
   background-image: url('foo.png')
}

由SCSS编译器自动转换为url('/assets/foo.png').

which automatically converted to url('/assets/foo.png') by the SCSS compiler.

这篇关于SCSS文件中的Rails 3.1资产url似乎未正确引用资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆