Deploying Ruby on Rails 3.1 Application to Heroku (dealing with some common errors)

Deploying Ruby on Rails 3.1 application to Heroku was not actually as straight forward as Heroku made it out to be. I had great experience with Heroku with Rails 2.x and Rails 3.0, but with Rails 3.1, I had to perform several extra steps in order to deploy successfully.

1. Using postgresql on Heroku production environment, while using sqlite or mysql in development environment

If my memory serves correctly, I'm pretty sure Heroku used to be able to perform rake db:migrate, even when the development database uses sqlite or mysql without extra configuration in Gemfile. This is not the case for Rails 3.1. Typically, deploying Heroku follows these steps.
$ heroku create
$ git push heroku master
$ heroku rake db:migrate

However, if you run the third step without Gemfile modification, you get this error.

$ heroku rake db:migrate
      rake aborted!
      Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add it to Gemfile.)

      Tasks: TOP => db:migrate => db:load_config
      (See full trace by running task with --trace)

Solution: Gem Install 'pg'

  1. Open your Gemfile
  2. Modify Gemfile this way
    group :production do
            gem 'pg'
          end
          group :development, :test do
            gem 'sqlite3' 
          end
  3. Install the bundle locally for your development environment.
    $ bundle install --without production

After performing these steps, you see that you can migrate now. Make sure after you edit your Gemfile, you push the changes to Heroku.

$ heroku rake db:migrate
    Migrating to CreatePosts (20111206055855)
    ==  CreatePosts: migrating ====================================================
    -- create_table(:posts)
       -> 0.0189s
    ==  CreatePosts: migrated (0.0199s) ===========================================

"We're sorry, but something went wrong."

So your application is now live online, but something is obvious wrong with this. To check out where it went wrong, perform this command.
$ heroku logs
The result will be something like the following.

2011-12-06T06:54:48+00:00 app[web.1]: Completed 500 Internal Server Error in 148ms
      2011-12-06T06:54:48+00:00 app[web.1]: 
      2011-12-06T06:54:48+00:00 app[web.1]: ActionView::Template::Error (application.css isn't precompiled):

This is caused by asset pipeline being disabled by default in Rails 3.1.

Solution: Enable asset pipeline compiling

  1. Open config/environments/production.rb
  2. Find this line: config.assets.compile = false
  3. Change the false to true. config.assets.compile = true

Something is still wrong with javascript runtime?

Solution: gem 'therubyracer'

  1. In Gemfile, put gem 'therubyracer' in the production group