Insights and outlooks on software development


On upgrading Rails from 2.3.5 to Rails 3 (via 2.3.8)

Friday, January 21, 2011 by Thomas L

I run a rather small and basic, but functional, web site for me and some friends on Rails. The site has been running on Rails 2.3.5 for a rather lengthy period, but a couple of weeks ago I decided to enter the new era of Rails 3. This is what I had to do in order to get things working.

While still remaining on Rails 2.3.5, I changed the project to use Bundler and made sure all of my tests ran well, and added a few extra ones.

I then upgraded to Rails 2.3.8. The upgrade was rather smooth, I didn't get any incompatibilities there, so I made sure everything worked, and then jumped on to the real upgrade, to Rails 3.

In Rails 3, I had trouble with the will_paginate gem (solved by upgrading the gem to version 3.0.pre2). I also had problems with Authlogic saying: "undefined method `to_key' for #". I solved this by using the form_for helper change mentioned here.

Since there are quite a few changes between Rails 2.3 and Rails 3, e.g. the new application.rb, I got help from the plugin rails-upgrade (instructions).

In controllers, old root redirects didn't work, i.e.

redirect_to root_url

To solve this, I had to define a named route in routes.rb like so:

match '/' => 'posts#index', :as => :root

Then I can use redirect_to like so:

redirect_to :root

Rails 3 has removed the markdown view helper method, so I had to use the formatize gem to get this working.

From Rails 3 onwards, all of the stuff output in the views are html escaped. Make sure that you stop escaping the user-generated things, and, more importantly, un-escaping the outputs in the views that should contain regular HTML.

Happy upgrade!