Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails 3.1 assets:precompile and images

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...

like image 929
fwalch Avatar asked Oct 04 '11 12:10

fwalch


People also ask

How do you Precompile rails assets?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What is assets Precompile?

rails assets:precompile is the task that does the compilation (concatenation, minification, and preprocessing). When the task is run, Rails first looks at the files in the config.assets.precompile array. By default, this array includes application.js and application.css .

What are assets in rails?

Generally asset is anything that browser loads after it gets the HTML page. Meaning javascript, css and any images. But as you pointed out there are two different image types in a rails project.


1 Answers

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.

like image 101
BaronVonBraun Avatar answered Sep 30 '22 08:09

BaronVonBraun