Monday, 16 January 2012

Rails 3.1 and Heroku

So I finally found some quiet moments to try out the latest Rails 3.1 incarnation. I had tried Rails 2 many years ago and found it quite enjoyable but figured I'd give Rails 3.x a try since I've been doing more development along these lines lately.

First of all I started with the Ruby on Rails Tutorial by Michael Hartl. This is a fine starting guide for most people and includes modern techniques including:
  • Using github to manage your code
  • Using Heroku to upload your code to a cloud host
I really wanted to try out the Heroku capabilities to see how far the state of the PaaS for Ruby had evolved as I had tried Google App Engine many years ago with both Java and Python and found it very inflexible.

So I created a little piece of code and called it Jakovo loosely based on the tutorial code just to try the Rails + Heroku combination. Its meant to be a task list, as I find task lists are always great for tutorials of this kind.

At the moment I've only gotten as far as creating a basic model with relationship between a user and task, and spent no time on making it look pretty. If I find more time I'll continue the work.

Extra Heroku Steps

When uploading to Heroku I found a couple of problems that I had to work-around that weren't clear  from the tutorial, so I'll outline these first.

First of all, Heroku uses Postgresql as a database, so my code I added the following to Gemfile (for bundler) to handle this:

group :production do
  gem "pg"
end

For asset support, I added therubyracer gem to my Gemfile. This uses the Chrome V8 javascript runtime.

group :assets do
  # A javascript runtime used during asset compilation in heroku
  gem 'therubyracer'

  gem 'sass-rails',   '~> 3.1.5'
  gem 'coffee-rails', '~> 3.1.1'
  gem 'uglifier', '>= 1.0.3'
end

Then I compiled the assets and checked them into git:

# bundle exec rake assets:precompile
# git add public/assets
# git commit -a

This is because you can't modify the filesystem remotely using the heroku interface.

To prepare the database I ran the following heroku command:

# heroku run bundle exec rake db:migrate

Which works the same as it does locally.

Deploying Using Heroku

The Heroku gem was a pleasure to use. After creating a Heroku account on heroku.com I added my keys:

# heroku keys:add

Then created an application:

# heroku create

This automatically added the necessary configuration to my local .git/config file that allowed me to deploy:

# git push heroku master

The deployment then walks through on the console all the steps it is performing before telling you it is done. I then navigated to the website:

# heroku open

Troubleshooting Heroku

To troubleshoot, Heroku includes some nice tools for this. Here are just a few I found handy during my attempts to dig into problems:

# heroku logs

This displays the remote logs in a nice colourful format.

# heroku pg:psql

Gives you a console to the remote Postgresql database, great for analysing your data.

# heroku apps:info

Shows detailed information on applications.

1 comment:

  1. therubyracer is strongly discouraged on Heroku now.

    https://devcenter.heroku.com/articles/rails3x-asset-pipeline-cedar#therubyracer

    ReplyDelete