新手问题 如何加载位于 assets/images 里的图片?

msl12 · September 01, 2016 · Last by msl12 replied at September 08, 2016 · 3508 hits
background: image-url('dandelion.jpg');
background-position: center;

如上,这是一段 css 代码,里面有引用位于app/assets/images的 jpg 图片,但是结果是这样的:

ActionController::RoutingError (No route matches [GET] "/images/dandelion.jpg"):
  actionpack (4.2.7.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.7.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'

我该如何才能让图片加载出来呢?用rake assets:precompile似乎不行..

会不会是拼错了?

我发现改成下面这个就对了:

background: url(/assets/dandelion.jpg);
background-position: center;

可是 sass-rails 里面有:

As a convenience, for each of the following asset classes there are
corresponding `-path` and `-url` helpers:
image, font, video, audio, javascript, stylesheet.

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

这里的 image-url 和我这里的 image-url 转变的不太一样,我这里却转出了/images/dandelion.jpg这样子的地址

这是为什么呢?

改成asset-url却可行.. 😧

#3 楼 @msl12 是否是扩展名不是 .css.scss 的关系呢?

#4 楼 @5swords 看了下,我的扩展名都是*.scss 😟

You need to Sign in before reply, if you don't have an account, please Sign up first.