Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails lib includes

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?

like image 840
stellard Avatar asked Jan 12 '10 11:01

stellard


People also ask

What goes in the lib folder?

The /lib directory contains kernel modules and those shared library images (the C programming code library) needed to boot the system and run the commands in the root filesystem, ie. by binaries in /bin and /sbin. Libraries are readily identifiable through their filename extension of *. so.

What is lib folder in rails?

In Rails's directory structure as far back as I can recall, there's always been a lib folder. This folder is for files that don't belong in the app folder (not controllers, helpers, mailers, models, observers or views), such as modules that are included into other areas of the application.

What does lib directory mean?

The lib folder is a library files directory which contains all helpful library files used by the system. In simple terms, these are helpful files which are used by an application or a command or a process for their proper execution. The commands in /bin or /sbin dynamic library files are located just in this directory.

What is lib in ruby?

It looks like "lib" contains all of the ruby code provided by the gem. So does "lib" mean "the source code files that constitute the library?" (It's hard to say if "lib" means "the library," since arguably the gemfiles are "part of the library," too.)


1 Answers

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".

like image 197
user664833 Avatar answered Sep 20 '22 11:09

user664833