I have the authentication code to be available over several Controllers. So I thought of putting the Authentication code in to a SuperClass and then make all the other controllers extend this SuperClass. I then got to know that we can add it in the ActionController class itself. How can we do that? Is there a way to change the pre defined class?
views:
217answers:
3
+1
A:
Actually all your controllers should already inherit from ApplicationController
, which in turn inherits from ActionController::Base
. And including authentication code into ApplicationController
is quite idiomatic, really.
neutrino
2010-02-07 19:09:19
I do know that code can be added into ApplicationController. But how do we do it? Do we create a new script file and declare a ApplicationController Class there? I am new to Rails so not much idea of how it can be done.
sana
2010-02-07 19:11:34
just check application_controller.rb (see sepp2k's answer for the path). and also make sure that all your other controllers inherit from ApplicationController.
neutrino
2010-02-07 19:13:46
A:
All controllers already extend ApplicationController
, so simply add an authorize method and a before_filter
calling that method in your ApplicationController
and you'll be all set for all your controllers.
JRL
2010-02-07 19:11:06
+1
A:
You should add the methods to the ApplicationController class which lives in app/controllers/application_controller.rb and is the direct superclass of all your project's controllers (assuming you created your controllers with script/generate and did not change the superclass).
sepp2k
2010-02-07 19:11:22