# Sharing code between some controllers? Staying DRY

**URL:** <https://www.ruby-forum.com/t/sharing-code-between-some-controllers-staying-dry/62117>\
**Category:** Rails\
**Created:** [May 31, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/sharing-code-between-some-controllers-staying-dry/62117 "2006-05-31T18:24:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 31, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/sharing-code-between-some-controllers-staying-dry/62117/1 "2006-05-31T18:24:26Z")

</div>

Hi,

I have four controllers: one for the store front and three for the  
store admin. In each controller I have copied and pasted exactly the  
same code. It is a method called redirect\_to() to override Action  
Controller’s redirect\_to(). Copy and paste is bad. I can think of two  
options but I don’t know how Rails will feel about them.

Option 1: Can I create an intermediate controller class like the  
following? If so, are their pit falls to this method?

class ApplicationController

end

class AdminController \< ApplicationController  
private  
def redirect\_to()  
end  
end

class ProductController \< AdminController

end

Option 2: I could create a flie in lib/ which contains the  
redirect\_to() method and then mix it into each controller. This option  
doesn’t feel as good for some reason. Also I don’t know what code  
would need to be in my controller file to do this.

Any ideas?

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 31, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/sharing-code-between-some-controllers-staying-dry/62117/2 "2006-05-31T18:31:04Z")

</div>

Well… I have used option 1 a lot. Feels fine.

class Admin::BaseController \< ApplicationController  
layout “admin”  
before\_filter :authorize, :except =\> :login  
end

class Admin::MessagesController \< Admin::BaseController

end

Allows me to do the before\_filter for all the /admin/something  
controllers in one place.

On 5/31/06, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class ProductController \< AdminController  
> Thanks,  
> Peter
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 31, 2006, 6:34pm UTC](https://www.ruby-forum.com/t/sharing-code-between-some-controllers-staying-dry/62117/3 "2006-05-31T18:34:15Z")

</div>

Hi,

Option 1 is the best i think - any reason why you’re overriding the  
redirect\_to() method though?

Steve

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 31, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/sharing-code-between-some-controllers-staying-dry/62117/4 "2006-05-31T18:59:41Z")

</div>

Not strange really - i just wondered if it might be better to just  
create your own method that calls redirect\_to rather than overriding the  
default. Further down the development of the application, you might  
find that you need the original working of redirect\_to which could cause  
you problems if you’ve overridden it - you’d need to call the superclass  
method explicitly.

Anyways - just a thought.

Steve

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 31, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/sharing-code-between-some-controllers-staying-dry/62117/5 "2006-05-31T18:47:12Z")

</div>

Hi Steve,

Thanks for the advice about controller inheritence.

On 5/31/06, Stephen B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> any reason why you’re overriding the  
> redirect\_to() method though?

Short story is I’m sending a redirect\_to as a response to an ajax  
request with status code 203. This will then prompt the browser to  
make a new ajax request. Maybe sounds strange but it seems to be  
working very well.

Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [May 31, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/sharing-code-between-some-controllers-staying-dry/62117/6 "2006-05-31T19:37:40Z")

</div>

On 5/31/06, Stephen B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Not strange really - i just wondered if it might be better to just  
> create your own method that calls redirect\_to rather than overriding the  
> default. Further down the development of the application, you might  
> find that you need the original working of redirect\_to which could cause  
> you problems if you’ve overridden it - you’d need to call the superclass  
> method explicitly.
> 
> Anyways - just a thought.

And a very good thought. I haven’t decided exactly what I will do with  
this redirect\_to issue. This project is research into some new UI  
ideas I haven’t seen before.

Either way I wanted to know about the controller inheritence.

Thanks again,  
Peter
