I recently put a Rails app I've been working on up on Heroku. When I push to my Heroku git repository it runs the asset compilation as part of generating the slug. It was failing here with the following error:
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port xxxx?
The problem is that the rake task, rake assets:precompile
, is trying to connect to the database and my app does not have access to config vars during slug compilation. I found this information in Heroku's troubleshooting documentation and added the prescribed fix in my application.rb
file:
config.assets.initialize_on_precompile = false
Now my assets get compiled correctly and the images in my gem's vendor/assets are usable.
Questions
- Why is the asset compilation rake task trying to connect to the database in the first place?
- Heroku also allows the assets to be compiled either locally and added to the git repo or during runtime. I believe these other two options won't run into the same problem as it only occurs during slug compilation. Is there any advantage in either of these?