Heroku上的Rails 4有一个奇怪的问题。当图像被编译时,它们被添加了散列,但从CSS中引用这些文件时没有适当的名称调整。这就是我的意思。我有一个叫logo。png的文件。然而,当它出现在heroku上时,它被视为:

/assets/logo-200a00a193ed5e297bb09ddd96afb953.png

然而,CSS仍然声明:

background-image:url("./logo.png");

结果是:图像无法显示。有人遇到过这种情况吗?如何解决这个问题?


链轮一起萨斯有一些漂亮的帮手,你可以用它来完成工作。链轮只会在样式表文件扩展名为.css时处理这些helper。或。css.sass。


图像特定的帮助器:

background-image: image-url("logo.png")

不可知论者的助手:

background-image: asset-url("logo.png", image)
background-image: asset-url($asset, $asset-type)

或者如果你想在css文件中嵌入图像数据:

background-image: asset-data-url("logo.png")

散列是因为资产管道和服务器优化缓存 http://guides.rubyonrails.org/asset_pipeline.html

试试这样做:

 background-image: url(image_path('check.png'));

古德勒克


我摆弄了几个小时后发现:

工作原理:

background-image: url(image_path('transparent_2x2.png')); 

// how to add attributes like repeat, center, fixed?

上面的输出类似于:“/assets/transparent_2x2-ec47061dbe4fb88d51ae1e7f41a146db.png”

注意前面的“/”,它在引号内。 还要注意stylesheet.css.scss中的scss扩展和image_path帮助器。图片在app/assets/images目录下。

不工作:

background: url(image_path('transparent_2x2.png') repeat center center fixed;

无效属性:

background:url(/assets/pretty_photo/default/sprite.png) 2px 1px repeat center fixed;

我最后的办法是把这些放到我的公共s3桶里,然后从那里加载,但最后还是有了一些东西。


只有这个片段不适合我:

background-image: url(image_path('transparent_2x2.png'));

但是要重命名stylename。SCSS到stylename.css.scss帮助我。


在Rails 4中,你可以像这样在你的.SCSS文件中引用位于assets/images/中的图像:

.some-div {
  background-image: url(image-path('pretty-background-image.jpg'));
}

当你以开发模式启动应用程序时(localhost:3000),你应该会看到如下内容:

background-image: url("/assets/pretty-background-image.jpg");

在生产模式下,你的资产将有缓存助手编号:

background-image: url("/assets/pretty-background-image-8b313354987c309e3cd76eabdb376c1e.jpg");

不知道为什么,但唯一对我有用的是使用asset_path而不是image_path,即使我的图像是在assets/images/目录下:

例子:

app/assets/images/mypic.png

在Ruby中:

asset_path('mypic.png')

在.scss:

url(asset-path('mypic.png'))

更新:

弄清楚了-原来这些资产助手来自sass-rails gem(我已经安装在我的项目中)。


在css中

background: url("/assets/banner.jpg");

尽管原始路径是/assets/images/banner.jpg,但按照惯例,您必须在url方法中只添加/assets/


这应该能让你每次都做到。

background-image: url(<%= asset_data_uri 'transparent_2x2.png'%>);

默认情况下,Rails 4不会为您的资产服务。要启用此功能,您需要进入配置/应用程序。Rb,加上这一行:

config.serve_static_assets = true

https://devcenter.heroku.com/articles/rails-4-asset-pipeline#serve-assets


有趣的是,如果我使用'background-image',它不起作用:

background-image: url('picture.png');

但是仅仅是“背景”,它就做到了:

background: url('picture.png');

没有一个答案是关于。css的。Erb扩展,如何引用图像。对我来说,我同时从事生产和开发工作:

2.3.1 CSS和ERB

资产管道自动评估ERB。这意味着如果你添加一个erb扩展到一个CSS资产(例如,application.css.erb),那么像asset_path这样的助手在你的CSS规则中是可用的:

.class { background-image: url(<%= asset_path 'image.png' %>) }

这将写入被引用的特定资产的路径。在这个例子中,在一个资源加载路径中有一个图像是有意义的,比如app/assets/images/image.png,这里将引用它。如果此图像已作为指纹文件在公共/资产中可用,则会引用该路径。

如果您想使用数据URI(一种将图像数据直接嵌入到CSS文件中的方法),您可以使用asset_data_uri帮助器。

.logo { background: url(<%= asset_data_uri 'logo.png' %>) }

这将在CSS源中插入格式化正确的数据URI。

注意,结束标记的样式不能是-%>。


在Rails 4中,只需使用.hero { 背景图片:url(“picture.jpg”); }在你的style.css文件,只要背景图像是隐藏在app/assets/images。


在某些情况下,以下几点也适用

Logo {background: url(<%= asset_data_uri ' Logo .png' %>)}

来源:http://guides.rubyonrails.org/asset_pipeline.html


参考Rails文档,我们可以看到有几种方法可以链接到css中的图像。请参阅第2.3.2节。

首先,如果您的css文件是sass文件,请确保它具有.scss扩展名。

接下来,你可以使用ruby方法,这真的很难看:

#logo { background: url(<%= asset_data_uri 'logo.png' %>) }

或者你可以使用更好的特定形式:

image-url("rails.png") returns url(/assets/rails.png)
image-path("rails.png") returns "/assets/rails.png"

最后,你可以使用一般形式:

asset-url("rails.png") returns url(/assets/rails.png)
asset-path("rails.png") returns "/assets/rails.png"

这招对我很管用:

background: #4C2516 url('imagename.png') repeat-y 0 0;

你可以添加到你的css .erb扩展名。Ej: style.css.erb

然后你可以放:

background: url(<%= asset_path 'logo.png' %>) no-repeat;

当使用宝石'sass-rails'时,在Rails 5, bootstrap 4中,以下对我有用,

在.scss文件:

    background-image: url(asset_path("black_left_arrow.svg"));

视图文件(例如.html.slim):

    style=("background-image: url(#{ show_image_path("event_background.png") })");