Rails 3.1 asset urls in SCSS files do not seem to be referencing the assets correctly

好久不见. 提交于 2019-12-09 19:40:39

问题


I just upgraded from Rails 3.0 to Rails 3.1.

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');
}

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);

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

Note that I am still working on development mode.

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

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

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

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

Update: my foo.css.scss file resides:

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

Does that make any difference?


回答1:


You may try:

.foo {
  background: url("/assets/foo.png")
}

should work fine. Hope it helps :)




回答2:


try

.classname{
  background: url(asset_path('/assets/image.png'))
}



回答3:


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

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

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




回答4:


.foo {
  background-image: asset-url('sub_dir/foo.png', asset);
}


来源:https://stackoverflow.com/questions/25638989/rails-3-1-asset-urls-in-scss-files-do-not-seem-to-be-referencing-the-assets-corr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!