Should a class/module in lib be required?
I just wrote a small module in my Rail 3.0.0 application lib folder:
module AdminFilters
def verify_is_admin
if current_user.nil? || current_user.role != User::Role::ADMIN
redirect_to :root, :alert => "You don't have enough permissions"
end
end
end
And in order to make it available for all my controllers :
class ApplicationController < ActionController::Base
protect_from_forgery
require "admin_filters"
include AdminFilters
end
If I remove the require line, rails complains like this :
uninitialized constant ApplicationController::AdminFilters
Is it the normal behavior ? I thought that any rb file in the lib folder was a开发者_运维百科uto-loaded by rails ...
Yes, it was auto-loaded in Rails 2.x.x, but Rails 3 doesn't load files from lib/
anymore. You should consider placing your files into the config/initializers
directory.
精彩评论