Ruby on Rails - Transition to rails 2.0

I put together a couple of simple rails sites that ran (just fine, apart from occasional problems when new version of lighttpd came out and broke things), from 2005 to 2008. In June of 2008, I noticed that I could now get rails as an rpm managed by yum, rather than via gem. So I got brave and whacked this in, and everything kept working .... but there were two things waiting to bite me.

First off, each rails project can independently specify which rails version it expects to be running on. This is done by setting the variable RAILS_GEM_VERSION in config/environment.rb. If you comment it out altogether (as I did), you will run with the latest version installed. In general, the gem system leaves all the versions in place side by side, on my system, when I do ls -l /usr/lib/ruby/gems/1.8/gems I see something like:

rails-1.1.4
rails-1.1.6
rails-1.2.3
rails-2.0.2
So I could in theory, specify any one of these via RAILS_GEM_VERSION, and keep running despite having installed a new version with API changes.

The second thing waiting to bite me was that until the web server handling rails (in my case lighttpd) was restarted, the switch to the new version really hadn't happend. I discovered this about a week later when I had to reboot my system for some other reason and my rails sites blew up.

So what is different with rails 2.0.2 ?

A lot of things. If you are starting a new project, you will discover the default database server is sqlite3, not MySQL (but you can just change config/database.yml to easily change this.

Rails 2 uses cookies on the client rather than files on the server to handle session information. Apparently there was (and still is) lots of discussion on this (which I won't chime in on), but to get your old site working with the new cookie based scheme, you will need to add some stuff to config/environment.rb that looks like this (it goes inside the Rails::Initializer.run block).

config.action_controller.session = {
    :session_key => '_xxxxx_session',
    :secret      => 'f7eb0b........ffa6824'
}
There is no doubt some way to generate the secret, but what I did is to rename my current project to put it aside out of harms way, then run rails xxxxxx and then find and copy the above lines into my project, then delete the bogus new project and rename the old one back where it belongs.
Yes, I suppose this is going the long way around the tree, but it got me up and going in less than 2 minutes time after 15 minutes of fruitless google searching.

on to countless nit-picky API changes.

First off, pagination is GONE!. The solution here is to grab the will_paginate helper and change to its entirely new (and nicer) interface. This is a win, but I sure wish it was part of the standard distribution rather than another gem to keep track of.

You no longer use start_form_tag and end_form_tag, but rather you use form_tag with a block (do and end).
Also, you get rid of the <%= and just use a <% with the new API.
If you had arguments to start_form_tag, the do goes after them. You can probably use curly braces instead of do and end to delimit the block.


Feedback? Questions? Drop me a line!

Ruby on Rails notes / tom@mmto.org