Ruby on Rails 3.1 assets:precompile and images

别来无恙 提交于 2019-12-01 01:14:10

问题


I can't get the Rails 3.1 asset pipeline precompilation to work in production mode. It always fails on images referenced within SCSS with an error like:

$ bundle exec rake assets:precompile RAILS_ENV=production 
  rake aborted!
  rails.png isn't precompiled
    (in /home/florian/AssetTest/app/assets/stylesheets/application.css.scss)

But when I look in the public/assets directory, the image is there, so it is precompiled:

  $ ls public/assets | grep rails 
    rails-dd352fc2630e5f9aa5685ef1d7fe5997.png

The SCSS file in this case just contains some test code:

/*
 * This is a manifest file that'll automatically include all the stylesheets available in this directory
 * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
 * the top of the compiled file, but it's generally better to create a new file per style scope.
 *= require_self
 *= require_tree . 
 */

body {
   background: #ffffff image-url('rails.png') no-repeat 0 center;
}

If I don't use the image-url helper in SCSS, but just url('/assets/rails.png'), precompilation works fine, and a manifest.yml file is generated in public/assets.

The interesting thing is: if I change the SCSS back to image-url('rails.png') and run another precompilation, it still works (I guess because the image is now already listed in the manifest file).

What am I doing wrong here? I don't really want to disregard the helper methods (as using them is The Way You Should Do It, right?), and I definitely don't want to create the manifest file manually...


回答1:


I've run into the same problem myself. This is apparently a bug in Rails 3.1.0, and will hopefully be fixed in short order...

In any event, in production.rb, you can try this:

config.assets.compile = true

You likely have it set to false, which it should be. However, having it set to false causes issues when you use asset helpers in SCSS as you're trying to do. Setting that value to true seems to properly allow compilation while using those helpers.

Take a look at this issue on github for some details.



来源:https://stackoverflow.com/questions/7647691/ruby-on-rails-3-1-assetsprecompile-and-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!