Monday 14 January 2013

Assets pre compilation error while deploying on heroku

In this post, I am going to address one of the problem I faced while using jquery in my ruby on rails project.

I was using Data Table and Multiselect jquery on my ruby on rails application. It was working fine on my local host. However when I pushed it to github and deployed it on Heroku. It started giving me the following assets pre compilation error.


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?


The reason for this compilation error can be be anything. I googled it around and found many solution like
  • Solution 1
    Changes in application.rb-In application.rb file, make config.assets.initialize_on_precompile = false. This option 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.
  • Solution 2
    Assets group in gemfile
    - keeping the jquery gems under assets group in your gemfile.
  • Solution 3
    Check your jquery and css
    - Make sure your css file and jquery files are correct and is in proper format. Even an extra semicolon (;) or brackets will fail the assets pre compliation. Even if you have downloaded a jquery from authentic source it may happen that it have some bugs which will work on local host but not on heroku. (Happened with me)
  • Solution 4
    Remove all the unused jquery from github- It might be possible that you might have been trying many jqueries with your application. You might have pushed this jqueries to github. But later on deleted it from your application in you local machine but didnt do same for the github.
    Use rm command to remove all the unused files.
    Note:- If you think that by deleting files from your local machine and pushing again to github will delete the files from the github also then you are wrong !!! (YES !! I was WRONG). I thought adding, commiting and pushing will remove the files from github also . (Since I am a fresher and new to github, I hope this type of mistake is acceptable :P )
I tried solution 1 2 & 3 but none if them was working. for me. During pre-compliation it was giving me the css error even after I removed all the css files from my local machine and pushed it to heroku.

So yes...I tried the solution 4 removed the conflicting css and jqueries from github and yeah there was no precompliation error !!

So what worked for me was to remove all the unused files css files and jquery files from your local machine and github.


No comments:

Post a Comment