Rails lib includes

I have a cryptic problem regarding modules defined in the dir lib directory

I have two files

#lib/authentication.rb module Authentication end #lib/test_module.rb module TestModule end 

In my application controller there is

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

Authentication module loads correctly, but TestModule fails

I get "uninitialized constant ApplicationController :: TestModule"

I'm at a dead end ... anyone?

EDIT: Does anyone know where I could debug this?

+7
include ruby-on-rails
source share
2 answers

Adding require 'lib/test_module' to the top of the ApplicationController file may help

+5
source share

As with Rails 3, be 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) 

Take a look here for more information on this and downloading subdirectories.

Also, presumably, " you should not use require in your rails application because it prevents ActiveSupport :: Dependencies from [un] loading this code correctly."

+20
source share

All Articles