奇怪的rails资产css图像行为(可能还有其他东西)

时间:2012-04-22 21:50:16

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 sass asset-pipeline

我有一个漂亮的vanilla rails应用程序,我的所有图像都在app / assets / images中,我的样式表在app / assets / stylesheets中。 在我的风格中我有:

body
  background: url(/assets/bg.png)
  //background: image-path('bg.png')--well, same thing also doesn't work

此图片不会显示,但如果我在视图中有=image_tag 'bg.png',则它可以正常工作(但最后没有生成的时间戳(不是bg.png?<buncha#s>)。

如果我转到localhost:3000/assets/bg.png,它就像我的所有图像一样 - 除了一个叫diag_bg.png的情况,在这种情况下,我得到一个超级奇怪的Errno::ENOTDIR Not a directory - project/app/assets/images/diag_bg。无论图像是否存在,我都不能将其用作css背景,即使我可以通过这种方式访问​​它。另外,firebug表示根本没有对css图像的GET请求。

-btw,这些图像都不在公共/图像中,除非我手动rake预编译资源(我从未在dev中将预编译设置为false)。但似乎这些图像甚至没有被使用......(有点混淆它们)

我也收到这些消息。我不确定它是否正常(logo_small.png到目前为止是single = image_tag):

Started GET "/assets/main.css?body=1" for 127.0.0.1 at 2012-04-22 14:31:33 -0700
Served asset /main.css - 304 Not Modified (0ms)
[2012-04-22 14:31:33] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true    

Started GET "/assets/logo_small.png" for 127.0.0.1 at 2012-04-22 14:31:33 -0700
Served asset /logo_small.png - 304 Not Modified (0ms)
[2012-04-22 14:31:33] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

任何帮助都会受到赞赏......我对资产感到非常沮丧。

1 个答案:

答案 0 :(得分:2)

您需要使用:

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

image-url帮助器是您想要的,您需要引用路径,而不需要将/assets放在前面。如果您的图片位于子文件夹中,则只包含一个文件夹,例如:

app/assets/images/icons/favorite.png

...将被引用为:

background: image-url("icons/favorite.png")

有关详细信息,请参阅https://github.com/rails/sass-rails