Rails 5如何从公用文件夹中的css文件访问公用文件夹中的图像

时间:2019-02-27 01:44:25

标签: ruby-on-rails

我有CSS文件

public/stylesheets/landing.css

使用标签时效果很好

<%= stylesheet_link_tag "/stylesheets/landing" %>

我也有这张图片

public/images/1-hero-image.jpg

现在,在css文件中,我试图引用此图像。

我尝试了以下变体:

background: url('1-hero-image.jpg');
background: url('/public/images/1-hero-image.jpg');
background: url('/images/1-hero-image.jpg');
background: url('images/1-hero-image.jpg');

它们似乎都不起作用。

感谢您能提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

background: url('/images/1-hero-image.jpg');在您的情况下就足够了。

但是it's better用于CSS和图像文件夹app/assets/