views:

217

answers:

3

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?

+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
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
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
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
+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
Thanks a lot. I missed seeing that there's already a ApplicationController class.
sana
One thing I forgot to ask- In the Routes.rb file do we have to use "ApplicationController" to specify the controller being used?
sana