Rails modules and routing

Go To StackoverFlow.com

1

I'd like to include TestModule in MyModule:

# in test_module.rb
module TestModule
    SOMETHING = [1, 2, 3]
end

# in my_module.rb
module MyModule
    include TestModule

    def my_method
        "testing"
    end
end

I'm receiving this error:

Routing Error: uninitialized constant MyModule::TestModule

I've double-checked the rails naming convention. Any idea why this isn't working?

More info: config.autoload_paths += ... in application.rb is commented out. However other modules in /lib are being loaded somehow.

More info 2: I think rails can't see the new file test_module.rb. If I add a new module to an existing file containing a module then including the new module works. Is there some sort of rails clean-up or refresh process for the $LOAD_PATH or something?

2012-04-04 18:14
by SundayMonday


2

You can also try with this:

# in my_module.rb

load 'test_module.rb'

module MyModule
   include ::TestModule

   def my_method
      "testing"
   end
end

to refer to top-level namespace.

2012-04-04 19:02
by shime


2

Try adding a "require" to the top of the file like this:

# in my_module.rb
require 'test_module'
module MyModule
  include TestModule
  ...
2012-04-04 18:46
by joelparkerhenderson
Ads