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.

About these ads

10 Responses to “Watch Your Namespace”

  1. topfunky Says:

    Did the 1.1.4 update fix this, or is this inherent to the nature of Ruby classes?

  2. Peat Says:

    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 …


  3. In Ruby, you cannot have a class and a module with the same name.

  4. Cesar Says:

    That sucks. I like rails but the cluttered namespace is one of the most disgusting gotchas in rails.


  5. […] 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 […]

  6. Mike Says:

    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.

  7. Harris Says:

    HI Mike

    I am totally agree with you, i had the prob in my site ComputersNext.com blog ….


  8. Right in Seattle’s renowned Pike Place Market is Inn
    at the Market; a 70-room boutique hotel that gives you easy access to the delightful array of shops, stalls, and eateries.
    When the sparks of a romance start, an Aries persona will need the
    whole works, pleasure, ardour and romance. You could be the best driver in the world, but when you.


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: