# Use code form one controller in another

**URL:** <https://www.ruby-forum.com/t/use-code-form-one-controller-in-another/86467>\
**Category:** Rails\
**Created:** [February 22, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/use-code-form-one-controller-in-another/86467 "2007-02-22T15:06:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 22, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/use-code-form-one-controller-in-another/86467/1 "2007-02-22T15:06:40Z")

</div>

Hi all,

Can I use code from one controller in another controller? If so, how  
do i do this?

Thanks  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 22, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/use-code-form-one-controller-in-another/86467/2 "2007-02-22T16:11:47Z")

</div>

On Thursday 22 February 2007, Tarscher wrote:

> Can I use code from one controller in another controller? If so, how  
> do i do this?

You don’t. You extract the common code into a third piece. Either an  
object that is instantiated in each of the controllers or a mixin  
module that is included in each.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 22, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/use-code-form-one-controller-in-another/86467/3 "2007-02-22T16:44:00Z")

</div>

Thanks for the reply,

Where in my rails structure do I put this common code directory wise?  
Somewhere in the app director?

Is there some tutorial on this?

Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 22, 2007, 5:27pm UTC](https://www.ruby-forum.com/t/use-code-form-one-controller-in-another/86467/4 "2007-02-22T17:27:14Z")

</div>

> Where in my rails structure do I put this common code directory wise?

> > Somewhere in the app director?  
> > Is there some tutorial on this?

[http://www.robbyonrails.com/articles/2007/02/09/extending-actioncontroller-part-two](http://www.robbyonrails.com/articles/2007/02/09/extending-actioncontroller-part-two)

-ed

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 22, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/use-code-form-one-controller-in-another/86467/5 "2007-02-22T21:37:46Z")

</div>

On 2/22/07, Alex T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t believe you can use the same code from a controller and in a  
> view - I’ve had to put a couple functions in both files. Though I may  
> be wrong.

You can.

class ApplicationController \< ActionController::Base  
helper\_method :current\_user, :logged\_in?  
end

This will make the ‘current\_user’ and ‘logged\_in?’ controller methods  
available to all views.

–  
Chris W.

> **[err.the\_blog](http://errtheblog.com)**
>
> Err the Blog: Ruby and Rails from the front.

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 22, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/use-code-form-one-controller-in-another/86467/6 "2007-02-22T17:14:42Z")

</div>

Tarscher wrote:

> Thanks for the reply,
> 
> Where in my rails structure do I put this common code directory wise?  
> Somewhere in the app director?
> 
> Is there some tutorial on this?
> 
> Stijn

There may be a more official way to do this, but what I’ve done is to  
create a file ‘application.rb’ in the controller directory and put  
shared functions in there. Works nice for validation or db queries etc.

I do the same for views / helpers by using a application\_helper.rb file.

I don’t believe you can use the same code from a controller and in a  
view - I’ve had to put a couple functions in both files. Though I may  
be wrong.
