Moving to Rails 2.0

I know … it’s like exercising, you know you’ll be better afterwards but you don’t feel like going trough the pain right now. Updating a framework can be a pain, here’s a short list summarizing what I had to do to migrate RefactorMyCode from Rails 1.2.3 to 2.0 (Preview). I hope this makes it easier for you!

0. Pistonize it!

Instead of using the classic way of freezing Rails in you app tree (rake rails:freeze:edge TAG=rel_2-0-0_PR), I’d recommend using Piston:

piston import vendor/rails

This way, you’ll be able to make changes to Rails code and keep them while updating your copy!

1. Update your stuff

Update configs, scripts and javascripts:

rake rails:update

2. Move your session config to environment.rb

Add this in your environment.rb and remove the session ... line in application.rb:

# Your secret key for verifying cookie session data integrity.
# If you change this key, all old sessions will become invalid!
config.action_controller.session = {
  :session_key => '_app_name_session_id',
  :secret      => 'your secret key'

To compute your secret key:

require 'digest/md5'
puts [now =, now.usec, rand(0), $$, 'you_app_name'].inject( { |md5, e| md5 << e.to_s }

3. Split environment.rb

Rails now recommend putting your specific config code in config/initializers. Move everything outside do |config| ... end into separate files. For starter, create config/initializers/inflections.rb and config/initializers/mime_types.rb and move what was in environment.rb. Split as much as you can in files with descriptive names.

4. Singleton resource, plural controller name

If you were using singleton resource: map.resouce :account, you’ll need to pluralize your controller name. In this case AccountsController. Not sure if it makes more sense, but follows the convention to pluralize controller names I guess…

5. Rename your views

Rails 2.0 brings a beautiful view naming convention:

[name].[content type].[template engine]

eg.: show.html.erb or create.js.rjs

Renaming manually can be pretty painful. Jamie created a Rake task for this.

6. Fix the plugins

Expects some plugins to break. In my case, asset_packager broke, but fixing it was simple: synching method signature for compute_public_path.

I’d recommend checking plugins compatibility before doing the jump. Read the doc. Or if you’re feeling adventurous, give it a try.

7. Add Request Forgery Protection

Cross-site request forgery can be hard to protect from. Not with Rails 2.0.

Add this to your application.rb

# See ActionController::RequestForgeryProtection for details
# If you're using the Cookie Session Store you can leave out the :secret
protect_from_forgery :secret => 'same_as_the_session_one'

And this to your config/environments/test.rb:

# Disable request forgery protection in test environment
config.action_controller.allow_forgery_protection    = false

Good luck!

Update: Paolo sent me an article he wrote for upgrading routes to 2.0 w/ a script, nice!


Filed under rails, tips, tutorial

20 responses to “Moving to Rails 2.0

  1. Pingback: Links for 29 October 2007(Rails, Dsl, JavaScript, CSS, TextMate) « exceptionz

  2. Woah! Long and tedious process. Makes me want to stay on 1.2 for a bit longer😉

    I’ll know where to look when I’m ready, though. Great article!

  3. I think only 1 and 4 are required, but what’s the point of updating if you’re not taking advantage of all the new cool and juicy things, right?

    Thx for the comment Carl!

  4. Nice one Marc! I just wish that line you listed to add to your test environment was better documented! Took me a long time to find when I made the move to edge a little while ago.

    There are a few other things to note:
    – If you have old-style named template files, the template resolution will be totally messed up. I had edit.haml, and edit.rjs, and only the RJS would show, even from a normal request. So, renaming your template files is more necessary than it looks!

    – Routes are different if you use nested resources. In 1.2.3, if you had a name_prefix, it would be in a slightly different place.
    i.e. 1.2.*: post.resources :comments, :name_prefix => “post_” # => post_edit_comment_path in 2.0, that becomes edit_post_comment_path.

    You also need to switch to the 2.0 stream of resource_controller🙂. And, of course, if you’re using resource_controller, you don’t have to worry about silly details like url helper names. /shameless plug

    Awesome guide Marc!

  5. Great stuff, thanks Marc-Andre.

    I have to this on a cuppel of projects as well… but i’m going to procrastinate a little more🙂

  6. @james: I had troubles w/ nested resource routes too. Although I chose the lazy option by doing :name_prefix => nil

    @frank: Not sure if it’s a good idea to swith now for big projects, RefactorMyCode only has a couple of controllers and models so it was pretty easy.

    Thx for the comments guys!

  7. Pingback: Links for 30 October 2007 (Rails, Ruby, JavaScript, CSS) « exceptionz

  8. Pingback: Monkeymagic » Links for October 29th

  9. Pingback: links for 2007-10-31 — More Light! More Light!

  10. Sapo

    When using piston and the command line above I get

    ” ‘svn’ : command svn info vendor/rails/.. resulted in an error: (runtimeError) ”

    This is in both my app directory and ruby\bin
    can anybody help

  11. you have to run this from your rails app dir

  12. Pingback: links for 2007-11-30 « Bloggitation

  13. Pingback: Andy Peters’ ecotone of thought » Road to Rails 2.0 (pt. 0)

  14. Hi Marc-Andre,

    As of today (dec 5 2007), Edge Rails is generating a 127 bytes secret for the CookieStore.

    Last week ago, it was generating an only 32 bytes long.

    There is many discussions about that security issue to store the secret in the cookie. I don’t know if I’m going to switch to the ActiveRecordStore, I’m not yet in production mode, but I’ll follow the issue.

    I don’t know if you might be interested in the new rake task patch to generate the key. There is a ticket for that:

  15. very cool, thx for sharing this info Philippe!

  16. I invite everybody to take a look at a screencast I’ve compiled yesterday. This is the classic Blog app built using Rails 2.0. I think it is the First Rails 2.0 full featured screencast around.

    See it “here”:

  17. great screencast Akita, thx for making this!

  18. Thanks – worked a treat🙂

  19. Cheers for the great guide. My version may have been older as it required a find and replace for @params/params and @flash/flash. To help out with view renaming softiesonrails provided this little gem:

  20. Pingback: Moving to Rails 2.0 « Marc-André Cournoyer’s blog - dhansson - dhansson

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s