在 CSS 中的 Ruby on Rails 2 中添加背景图像
我在 ruby 中生成了一个脚手架,我想在网站的每个页面上插入一个背景图像,但我不确定图像的链接应该是什么。
我的图像位于 app/assets/images/"dep.jpg"
这是我所拥有的,但它不起作用:
background-image:url('../images/dep.jpg');
}
有帮助吗?谢谢
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.
my image is in app/assets/images/"dep.jpg"
this is what i have but it isnt working:
background-image:url('../images/dep.jpg');
}
Any help? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
背景: url('/assets/image_name.jpg');
这在 Rails 4.0 中对我有用
background: url('/assets/image_name.jpg');
This worked for me in Rails 4.0
您可以肯定使用绝对路径:
You can use absolute path for sure:
如果您使用的是现代版本的rails(我相信是3.1或更高版本),您可以使用:
由于css也位于您的资产文件夹中
..
自动引用资产文件夹。If you are using a modern version of rails (3.1 or later I believe), you can use:
Since the css also lives in your assets folder
..
automatically refers to the assets folder.在Rails 3.1中,路径实际上是“/assets/dep.jpg”:
如果将scaffold.css文件转换为Sass文件(重命名为scaffold.css.scss),那么您可以使用帮手:
In Rails 3.1, the path will actually be `/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:Rails 中的网络服务器将
public
文件夹作为应用程序的基础。因此,它在/public/images/
下查找特定图像,而不是app/assets/images/"dep.jpg"
&因为它不在那儿,所以你无法获取图像。尝试将您的图像放入/public/images/
文件夹中,然后它就可以工作。编辑:如果您使用的是 Rails 3.1,那么这会有所不同,因为 Rails 3.1 对应用程序的资产使用资产管道的概念,因此路径
app/assets/images/dep。 jpg
显然可以工作。The webserver in rails takes
public
folder as the base of the application. Hence its looking for the specific image under/public/images/
instead ofapp/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.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.如果您的图像位于
app/assets/images/dep.jpg
中,那么在 Rails 中的 css 文件中,您应该编写background: url('/assets/dep.jpg');< /code>
您不必以这种方式将图像放入公共文件夹中。
If your image is in
app/assets/images/dep.jpg
, then in the css file in rails you should writebackground: url('/assets/dep.jpg');
You don't have to put the image in the public folder this way.