7

I have a puzzling issue regarding modules defined in the lib dir

I have two files

#lib/authentication.rb

module Authentication

end


#lib/test_module.rb

module TestModule

end

In my application controller I have

 class ApplicationController < ActionController::Base
     include Authentication
     include TestModule
 end

The Authentication Module loads properly but the TestModule does not

I get "uninitialized constant ApplicationController::TestModule"

I am stumped... anyone?

EDIT: Does anyone know where I could look to debug this?

stellard
  • 5,162
  • 9
  • 40
  • 62

2 Answers2

21

As of Rails 3, make sure to add the lib directory to config.autoload_paths in config/application.rb, so that the file containing your module is read and the module is loaded.

config.autoload_paths += %W(#{config.root}/lib)

Look here for more info on this and loading subdirectories.

Also, supposedly "you shouldn't use require within a rails app, because it prevents ActiveSupport::Dependencies from [un]loading that code properly".

Community
  • 1
  • 1
user664833
  • 18,397
  • 19
  • 91
  • 140
6

Adding require 'lib/test_module' at the top of your ApplicationController file might help

Veger
  • 37,240
  • 11
  • 105
  • 116
  • 2
    Tried that one and this is what I got no such file to load -- lib/test_module (MissingSourceFile) – stellard Jan 12 '10 at 19:32
  • 2
    Are you sure that the file is in the right place?? Since the require statement is unable to find it... – Veger Jan 12 '10 at 22:39
  • 1
    I was sure that it was in the right place but I double checked again. For some reason the file name was test_module.r?b. This is odd since it displays in TextMate(where I created the file) as .rb. I renamed the file and it worked. Thank you for your time on this, I was so puzzled I needed another set of eyes on it. – stellard Jan 13 '10 at 00:57