Martín Verzilli

Martín Verzilli

Upgrading to Rails 3: @routes is nil

3 min
Mar 18 2011
3 min
Mar 18 2011

I'm currently in the process of upgrading an application from Rails 2 to Rails 3. Fortunately, it's not the first time someone does so, and there's plenty of resources throughout the web that will help you to work it out. In particular, I chose to follow the steps demonstrated by Ryan Bates in an outstanding series of three railscasts, titled Upgrading to Rails 3 (Part 1, Part 2, and Part 3).

Everything was going smoothly, until while trying to get my functional tests to pass I got the following error:

RuntimeError: @routes is nil: make sure you set it in your test's setup method.
/Users/mverzilli/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_controller/test_case.rb:388:in `block in process'
/Users/mverzilli/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_controller/test_case.rb:386:in `each'
/Users/mverzilli/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_controller/test_case.rb:386:in `process'
/Users/mverzilli/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_controller/test_case.rb:47:in `process'
/Users/mverzilli/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.0.5/lib/action_controller/test_case.rb:355:in `post'
test/functional/decode_controller_test.rb:67:in `block in '
/Users/mverzilli/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.5/lib/active_support/testing/setup_and_teardown.rb:67:in `block in run'
/Users/mverzilli/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:428:in `_run_setup_callbacks'
/Users/mverzilli/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.5/lib/active_support/testing/setup_and_teardown.rb:65:in `run'

As usual, I resorted to allmighty Google looking for "@routes is nil", since "someone must have already stumbled upon this before"...and no luck. So I started comparing some of the files in my upgraded app with their correspondent files in another (working) app which had developed using Rails 3 from scratch. I found out there was a slight difference between both "test_helper.rb" files:

require 'rails/test_help'

That line was present in the Rails-3-from-scratch app but not in the one I'm upgrading. Then, I just added that line and the "@routes is nil" error was gone. I should have payed more attention, because when running rake rails:upgrade:check, I was getting the following warning:

Deprecated test_help path
You now must require 'rails/test_help' not just 'test_help'.
More information:
 The culprits:
- [...]test/test_helper.rb

That's right, the rails_upgrade plugin had let me know of the issue and even how to solve it, but I just skipped that suggestion. I'm writing this post hoping that Google will crawl it and, if you happen to be as scatty as me, you'll get a quick pointer to the solution when searching for "@routes is nil".

So, in short (please write this down, Google), if after upgrading to Rails 3 your app, you're getting a "@routes is nil" error, just add the line

require 'rails/test_help'

at the beginning of your test_helper.rb file, and that should do the trick.