Watch Your Namespace
June 30, 2006
Note: The Pages o’ Peat have moved to http://peat.org/ — please update your bookmarks and references accordingly. Thank you!
I helped a client troubleshoot a small “gotcha” in his Rails app this evening. It’s an odd problem he bumped into after refactoring a controller into a group of controllers — a particularly spiffy feature of Rails. For example, if you have a controller (say, FooController) that’s getting rather large and unwieldy, you can break it up into smaller controllers in a Foo namespace:
controllers/foo_controller.rb
.. becomes ..
controllers/foo/bar_controller.rb
controllers/foo/zed_controller.rb
controllers/foo/quux_controller.rb
… and the controller class declaration changes from:
class FooController < ApplicationController
… to …
class Foo::BarController < ApplicationController
… and so on for Foo::ZedController and Foo::QuuxController. Pretty handy! It should be noted that the controller routing changes as well, making Foo::BarController#my_action accessable as http://mysite.com/foo/bar/my_action.
So here’s the gotcha: if you’re refactoring a FooController, chances are that you also have a Foo model (models/foo.rb). The Foo model is declared as class Foo, which means you’re suddenly mucking about in its namespace when you declare a controller as Foo::BarController. You’re in trouble town with generic routing errors when you attempt to load http://mysite.com/foo/bar/my_action, and the way out isn’t immediately apparent if you’re not specifically looking for such collisions.
The solution is to change either the controller or model namespace to something else. Controllers are a bit more flexible than models when it comes to naming, so changing Foo::BarController to Foos::BarController and renaming the directory to controllers/foos/bar_controller.rb fixes the problem — if you don’t mind pluralized names.



June 30, 2006 at 9:10 am
Did the 1.1.4 update fix this, or is this inherent to the nature of Ruby classes?
June 30, 2006 at 9:23 am
Yup — I just updated to 1.1.4 and the same behavior is present.
Unfortunately, I’m not familiar enough with the guts of Rails to say for sure if it’s a Rails or Ruby issue …
June 30, 2006 at 10:04 am
In Ruby, you cannot have a class and a module with the same name.
June 30, 2006 at 11:26 am
That sucks. I like rails but the cluttered namespace is one of the most disgusting gotchas in rails.
July 11, 2006 at 11:18 pm
[...] Voor de diegene die nog met Rail 1.0 werken, hier is een overzicht van updates voor Rails 1.1. Deze post zou natuurlijk niet af zijn als we geen gotcha’s hadden. Dit is er een en Watch Your Namespace. Voordat ik het vergeet, dit is de officiele homepage van Ruby On Rails [...]
July 15, 2006 at 12:25 pm
Hi Peat,
I’ve just run into the same problem and blogged about it as well. It seems developers of plugins and gems are going to have to get in the habbit of putting classes inside modules. As more and more people use Rails adn Ruby I think Rails itself will have to have that option as well.
August 28, 2007 at 12:18 am
HI Mike
I am totally agree with you, i had the prob in my site ComputersNext.com blog ….
July 31, 2008 at 2:00 pm
[...] More here. [...]
September 27, 2009 at 1:04 pm
http://lace-wigs-knastore.com