sass-rails helpers"image-url","asset-url"在rails 3.2.1中不起作用

pat*_*ick 45 ruby-on-rails sass ruby-on-rails-3 asset-pipeline

我在3.2.1,sass-rails-3.2.4和sass-3.1.15 ......

资产管道的文档说:

asset-url("rails.png", image) becomes url(/assets/rails.png)
image-url("rails.png") becomes url(/assets/rails.png)
Run Code Online (Sandbox Code Playgroud)

...

所以我做了以下文件:

# app/assets/stylesheets/public/omg.css.sass

body
  background: asset-url('snake.gif', image)

#lol
  background: image-url('snake.gif')
Run Code Online (Sandbox Code Playgroud)

当我访问localhost:3000/assets/public/omg.css时,我得到:

body {
  background: asset-url("snake.gif", image); }

#lol {
  background: image-url("snake.gif"); }
Run Code Online (Sandbox Code Playgroud)

...我也尝试将文件更改为omg.css.scss并将语法更改为:

# app/assets/stylesheets/public/omg.css.scss

body {
  background: asset-url('snake.gif', image);
}

#lol {
  background: image-url('snake.gif');
}
Run Code Online (Sandbox Code Playgroud)

但得到相同的结果......有没有人知道为什么这些助手不工作?

小智 33

尽管文档中说的是,看起来rails 3.2.6中的默认选项允许您在CSS中使用更少的路径信息.例如../app/assets/images/rails.png,example.css.scss文件中的引用类似于:

background: white url(rails.png) repeat-y;

你不包括image-url或者asset-url你的scss(据我所知),只是简单url(your_image.png).这些文档似乎只是对它在后台所做工作的解释.

  • 仅使用`url(rails.png)`不使用资产文件的指纹版本.在我们的.css.scss文件中,我们需要使用`background:image_url('my-image.png')`. (48认同)
  • 如果你需要获得指纹输出,请确保你**不要这样做:`rake assets:precompile`.您需要设置生产环境,例如`RAILS_ENV =生产佣金资产:预编译`,否则它只会为开发模式生成资产链接. (8认同)
  • ^^ 该评论确实如此!!!为我节省了数小时的头痛`RAILS_ENV=production rake assets:precompile` (2认同)

For*_*est 11

当我遇到这个问题时,那是因为我没有在资产管道中包含css文件以进行预编译.结果,它将在运行时生成.因为sass-rails gem通常位于:assets组中,所以在运行时生成css文件时助手不可用.

尝试将以下行添加到application.rb(或production.rb):

config.assets.precompile += %w( public/omg.css )
Run Code Online (Sandbox Code Playgroud)

我在这篇文章中找到了修复,包括在将文件添加到预编译器时命名文件.


Rya*_*yan 6

如果您以前已将应用程序更新为Rails 3.1,请确保已更改application.rb文件

# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
Run Code Online (Sandbox Code Playgroud)

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require *Rails.groups(:assets => %w(development test))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end
Run Code Online (Sandbox Code Playgroud)

请参阅此railscast以升级到Rails 3.1并添加资产管道.

更新: Rails 4回归旧的方式.谢谢Aaron Gray!

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
Run Code Online (Sandbox Code Playgroud)

  • 一旦你去了Rails 4,你会想要回到使用Bundler.require(:default,Rails.env)的旧方法.请参见http://railscasts.com/episodes/415-upgrading-to-rails-4. (4认同)