How to deal with image references in CSS in a Rails 3.1 app

大兔子大兔子 提交于 2019-12-23 04:49:05

问题


I just upgraded my rails app to 3.1 and I'm making use of the new asset pipeline. With the pipeline in place it's imperative that any static references made in your application utilize the rails helper methods, ie, asset_path(...), img_tag(), etc....

Here's the question, how do I fix the static references in my css files?


回答1:


In Rails' SASS files, there's an image-url() function you can use like so:

.page {
    background: #eee image-url('somefile.png');
}

Rails will look for that file in all the asset images directories, such as app/assets/images/somefile.png.

Check the readme for sass-rails for more helper methods, as well as the Asset Pipeline Rails guide for further documentation and explanation.




回答2:


If you work with in engine or plugin, this might not work. In any case you can always use the generic method:

asset-url("rails.png", image) 


来源:https://stackoverflow.com/questions/7375568/how-to-deal-with-image-references-in-css-in-a-rails-3-1-app

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