Heroku and Rails 3.2 asset:precompile error

February 5th, 2012 at 10:34 pm • permalink18 comments

This article targets Rails >= 3.2

The article was written as of Rails 3.2. The information contained in this page might not apply to different versions.

I have a very simple Rails 3.1 application, deployed on Heroku. Just after upgrading it to Rails 3.2, the deploy to Heroku stopped working properly.

More specifically, the rake asset:precompile task was failing during slug compilation.

       Your bundle is complete! It was installed into ./vendor/bundle
       Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       rake aborted!
       could not connect to server: Connection refused
       Is the server running on host "127.0.0.1" and accepting
       TCP/IP connections on port 5432?

       Tasks: TOP => environment
       (See full trace by running task with --trace)
       Precompiling assets failed, enabling runtime asset compilation
       Injecting rails31_enable_runtime_asset_compilation
       Please see this article for troubleshooting help:

http://devcenter.heroku.com/articles/rails31_heroku_cedar#troubleshooting

The Heroku assets:precompile toubleshooting page contains several explanations, but none of these was my case. In fact, my application was working properly with Rails 3.1.

It turns out there are some changes in the Rails 3.2 initialization process that conflicts with Heroku slug compilation. The solution is very simple. All you have to do is to set the Rails 3.2 initialize_on_precompile configuration to false in your application.rb file.

config.assets.initialize_on_precompile = false

This option is new in Rails 3.2 and prevents the Rails environment to be loaded when the assets:precompile task is executed. Because Heroku precompile assets before setting the database configuration, you need to set this configuration to false or you Rails application will try to connect to an unexisting database.

       Your bundle is complete! It was installed into ./vendor/bundle
       Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
-----> Rails plugin injection
       Injecting rails_log_stdout
       Injecting rails3_serve_static_assets
...

This configuration is also documented in the Precompiling Assets section of the new Rails 3.2 asset pipeline guide.

Filed in Programming • Tags: , ,

Comments

wojtek says:

You can also use Heroku plugin:
http://devcenter.heroku.com/articles/labs-user-env-compile

Andrew says:

Yep, that heroku labs plugin was the fix for me: https://devcenter.heroku.com/articles/labs-user-env-compile

Keil Miller says:

@wojtek “The features added through labs are experimental and may change or be removed without notice.” That tells me that if your website is not experimental, do not rely on this plugin.

@simone carletti Thank you so much! You saved my day. It worked like a charm. :)

KendallB says:

Awesome! Thanks for the post – totally fixed it.

Markus says:

Great! Thanks for sharing!

Raykin says:

Save me tonight.Thanks.

Cristian Vasquez says:

Thanks man really helpful

Mike Furtak says:

Thanks for posting this. You’ve saved me a bunch of searching.

roman says:

You saved my day, thanks!

microspino says:

Sei un gigante. Mi hai salvato un pomeriggio. Grazie.

Matt Wynne says:

It’s crucial you put this config setting into config/application.rb and not just have it in config/environments/production.rb

I didn’t realise there was a different (I assumed the whole thing was read before the app loads) but apparently there is.

Walter Yu says:

Great write up, it helped get me going on heroku deployment using Railsapps templates.

Lucas Pottersky says:

thank yooou!!!

i was having this problem when trying to setup Mongoid/Mongo Mapper with my Rails 3.2.8 app.

Had the same problem and found your page via Google. Thanks and have a great 2013!

David Parker says:

Worked for me (Rails 3.2.11), thanks!

Carlos says:

Thanks!

Yuri says:

You should keep in mind this warning from assets guide:

If you set config.assets.initialize_on_precompile to false, be sure to test rake assets:precompile locally before deploying. It may expose bugs where your assets reference application objects or methods, since those are still in scope in development mode regardless of the value of this flag.

Eric Saboia says:

Works like a charm. Thanks!

Add a Comment




Follow Me
    Random Quote