在 CSS 中的 Ruby on Rails 2 中添加背景图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9024252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a background image in Ruby on Rails 2 in CSS
提问by Dan
I generated a scaffold in ruby and I want to insert a background image to every page on the site but Im not sure what the link to the image should be.
我用 ruby 生成了一个脚手架,我想在网站上的每个页面中插入一个背景图片,但我不确定图片的链接应该是什么。
my image is in app/assets/images/"dep.jpg"
我的形象在 app/assets/images/"dep.jpg"
this is what i have but it isnt working:
这是我所拥有的,但它不起作用:
background-image:url('../images/dep.jpg');
}
Any help? thanks
有什么帮助吗?谢谢
采纳答案by uday
The webserver in rails takes public
folder as the base of the application. Hence its looking for the specific image under /public/images/
instead of app/assets/images/"dep.jpg"
& since its not there over there you cannot get the image. Try to put your image in /public/images/
folder then it would work.
rails 中的网络服务器以public
文件夹作为应用程序的基础。因此,它在寻找特定图像/public/images/
而不是app/assets/images/"dep.jpg"
& 因为它不在那里你无法获得图像。尝试将您的图像放在/public/images/
文件夹中,然后它就会起作用。
Edit:If you are using rails 3.1 then this would be different as Rails 3.1 uses the concept of asset pipeline for the assets of your application so then then path app/assets/images/dep.jpg
would obviously work.
编辑:如果您使用的是 rails 3.1,那么这将有所不同,因为 Rails 3.1 将资产管道的概念用于您的应用程序的资产,因此路径app/assets/images/dep.jpg
显然可以工作。
回答by Dylan Markow
In Rails 3.1, the path will actually be `/assets/dep.jpg':
在 Rails 3.1 中,路径实际上是 `/assets/dep.jpg':
background-image: url(/assets/dep.jpg);
If you convert your scaffold.css file to a Sass file (rename to scaffold.css.scss
) then you can use the helper:
如果将 scaffold.css 文件转换为 Sass 文件(重命名为scaffold.css.scss
),则可以使用帮助程序:
background-image: image-url("dep.jpg");
回答by uday
If your image is in app/assets/images/dep.jpg
, then in the css file in rails you should write background: url('/assets/dep.jpg');
如果你的图片在app/assets/images/dep.jpg
,那么你应该在 rails 的 css 文件中写background: url('/assets/dep.jpg');
You don't have to put the image in the public folder this way.
您不必以这种方式将图像放入公用文件夹中。
回答by laffan
background: url('/assets/image_name.jpg');
背景: url('/assets/image_name.jpg');
This worked for me in Rails 4.0
这在 Rails 4.0 中对我有用
回答by Oleksandr Skrypnyk
You can use absolute path for sure:
您可以肯定地使用绝对路径:
background-image:url('/images/dep.jpg');
回答by Wylliam Judd
If you are using a modern version of rails (3.1 or later I believe), you can use:
如果您使用的是现代版本的 rails(我相信是 3.1 或更高版本),您可以使用:
background: url('../dep.jpg');
Since the css also lives in your assets folder ..
automatically refers to the assets folder.
由于 css 也位于您的资产文件夹中,因此会..
自动引用资产文件夹。