Forum: Ruby on Rails Controller modules;- Problems if already defined

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 james.adam (Guest)
on 2005-11-18 17:07
(Received via mailing list)
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
27c170f482104299af279902be0a9c26?d=identicon&s=25 trevor (Guest)
on 2005-11-18 17:43
(Received via mailing list)
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
This topic is locked and can not be replied to.