Controller modules;- Problems if already defined

Rails lets me nest my controllers in modules, so I can happily write:

class MyModule::MyController < ApplicationController

…in a file in /app/controllers/my_module/my_controller.rb, and Rails
will load it nicely at http://myapp/my_module/mycontroller.

However, it looks like the dependency mechanism will only work
properly if the MyModule module has not already been defined (by, say,
a plugin). I can understand why Rails might be trying to create the
module for me, so that I wouldn’t have to have code like:

module MyModule
# empty
end

…somewhere in my application such that I could then define classes
(controllers) within that module. However, I don’t see why Rails
should insist that the module doesn’t already exist if I want to put
some controllers in it.

Can anyone comment? Is this a bug, or is this behaviour necessary?

The specific error is:

Cannot load module MyModule: Object::MyModule is set to MyModule

from

/opt/local/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:91:in
const_load!' /opt/local/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:80:in each’
/opt/local/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:80:in
const_load!' /opt/local/lib/ruby/gems/1.8/gems/activesupport-1.2.3/lib/active_support/dependencies.rb:72:in const_missing’
routing.rb:236:in traverse_to_controller' generated/routing/recognition.rb:3:in eval’
generated/routing/recognition.rb:3:in `recognize_path’

Cheers

  • James

James,

I can only assume that it’s because the ‘MyModule’ class is expected to
be a LoadingModule which does automatic loading via const_missing
slightly different from Module.const_missing.

And it might have something to do with being able to discard the loaded
consts when Controllers.clear! is called.

Regards
Trevor