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?
Asked
Active
Viewed 1,796 times
3 Answers
1
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.

alex.zherdev
- 23,914
- 8
- 62
- 56
-
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. – MohamedSanaulla Feb 07 '10 at 19:11
-
just check application_controller.rb (see sepp2k's answer for the path). and also make sure that all your other controllers inherit from ApplicationController. – alex.zherdev Feb 07 '10 at 19:13
1
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
- 363,768
- 54
- 674
- 675
-
Thanks a lot. I missed seeing that there's already a ApplicationController class. – MohamedSanaulla Feb 07 '10 at 19:15
-
One thing I forgot to ask- In the Routes.rb file do we have to use "ApplicationController" to specify the controller being used? – MohamedSanaulla Feb 09 '10 at 07:18
0
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
- 76,767
- 18
- 98
- 146