# Two controllers needing the same method

**URL:** <https://www.ruby-forum.com/t/two-controllers-needing-the-same-method/103271>\
**Category:** Rails\
**Created:** [May 3, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/two-controllers-needing-the-same-method/103271 "2007-05-03T17:51:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 3, 2007, 5:51pm UTC](https://www.ruby-forum.com/t/two-controllers-needing-the-same-method/103271/1 "2007-05-03T17:51:58Z")

</div>

Two of my controllers need access to same method. I instantly put them  
in  
ApplicationController and the problem was solved.

Now I’m thinking my ApplicationController is getting polluted because  
there  
are many cases in which a method is needed by two or three controllers.  
What  
are the other ways to handle such cases.

One case could be to have a base controller and then to extend these  
controllers so that they get the common methods. Well I don’t like it  
that  
much.

Any thoughts.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 3, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/two-controllers-needing-the-same-method/103271/2 "2007-05-03T17:53:34Z")

</div>

You could have a module with the common methods and include it in  
controllers needing those methods

Fred

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [May 6, 2007, 4:26am UTC](https://www.ruby-forum.com/t/two-controllers-needing-the-same-method/103271/3 "2007-05-06T04:26:22Z")

</div>

Raj S. wrote:

> Two of my controllers need access to same method. I instantly put them  
> in  
> ApplicationController and the problem was solved.
> 
> Now I’m thinking my ApplicationController is getting polluted because  
> there  
> are many cases in which a method is needed by two or three controllers.  
> What  
> are the other ways to handle such cases.
> 
> One case could be to have a base controller and then to extend these  
> controllers so that they get the common methods. Well I don’t like it  
> that  
> much.
> 
> Any thoughts.

Group the methods into categories. Put each category in a file in  
#{RAILS\_ROOT}/lib/aplication\_controller (or where ever).

Easy (but less orginized) way. In each file put:

AplicationController \<ActionController::Base  
#function go here  
end

and require the files in Application.rb

More complex (but Ruby like ) way. Each file defines a module with the  
functions. Include the module in either ApplicationController or  
individual Controllers.

JFM
