# What is the best way to reuse a controller's action and views?

**URL:** <https://www.ruby-forum.com/t/what-is-the-best-way-to-reuse-a-controllers-action-and-views/127101>\
**Category:** Rails\
**Created:** [January 24, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-reuse-a-controllers-action-and-views/127101 "2008-01-24T23:47:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@Parama](https://www.ruby-forum.com/u/Parama)\
**Post date:** [January 24, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-reuse-a-controllers-action-and-views/127101/1 "2008-01-24T23:47:26Z")

</div>

What is the best way to reuse a controller’s actions and views?

Lets say I have an application that has controller:  
SampleController\<ApplicationController

This controller would be accessable from the following url  
http:localhost:3000/sample/blah\_action

I need to create another controller that has the actions of this  
controller, plus some more actions of its own

If I created another controller: ImageController \< SampleController

Then this url : [http://localhost:3000/sample/image/blah\_action](http://localhost:3000/sample/image/blah_action) would  
take me to the ImageController if the actions exist exists in it.  
however if the action doesn’t exist, it should use the parent’s  
actions and views.

What is the best way to do this?

Thanks

---

<div class="post-metadata">

**Author:** ![Parama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@Parama](https://www.ruby-forum.com/u/Parama)\
**Post date:** [January 25, 2008, 12:16am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-reuse-a-controllers-action-and-views/127101/2 "2008-01-25T00:16:46Z")

</div>

I’d extract the common actions in a module and include it in both  
controllers.

---

<div class="post-metadata">

**Author:** ![Parama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@Parama](https://www.ruby-forum.com/u/Parama)\
**Post date:** [January 25, 2008, 3:01am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-reuse-a-controllers-action-and-views/127101/3 "2008-01-25T03:01:15Z")

</div>

But I need the views associated with that action also. how would you  
include the views?

Are there any way to do this more cleanly through controller  
inheritance?

---

<div class="post-metadata">

**Author:** ![Parama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@Parama](https://www.ruby-forum.com/u/Parama)\
**Post date:** [January 25, 2008, 3:08am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-reuse-a-controllers-action-and-views/127101/4 "2008-01-25T03:08:30Z")

</div>

I am confused, you pretty much seemed to answer your own question. There  
is no reason you can’t just use controller inheritance:

class ParentsController \< AC::Base  
def show  
# code here  
respond\_to do |format|  
format.html { render :template =\> ‘parent/show’ }  
end  
end  
end

and then make /views/parents/show.html.erb

and then to extend:

class ChildrensController \< ParentsController

end

---

<div class="post-metadata">

**Author:** ![Parama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@Parama](https://www.ruby-forum.com/u/Parama)\
**Post date:** [January 25, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-reuse-a-controllers-action-and-views/127101/5 "2008-01-25T16:37:18Z")

</div>

Check out RailsEngines: [http://rails-engines.org/introduction](http://rails-engines.org/introduction)

---

<div class="post-metadata">

**Author:** ![Parama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@Parama](https://www.ruby-forum.com/u/Parama)\
**Post date:** [January 25, 2008, 12:06pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-to-reuse-a-controllers-action-and-views/127101/6 "2008-01-25T12:06:50Z")

</div>

Hi,

I has the same issue when I wanted a SOAP controller  
(ActionWebService) to reuse my Restful logic in the normal  
controllers.  
Inheritance was not an option, because i needed logic from mulitple  
Restful controllers (mixin would override shared method names, so that  
didn’t work).  
I ended up using a render\_component statement (with response format  
XML) from the SOAP Controller towards the Restful controller, which is  
really ugly because that means I was actually pretending the SOAP  
controller to be an external application. But it works fine en it  
helps me keep my code DRY (but slower).

If anybody has a better idea to solve this please let me know.

Regards,  
Bas

On Jan 25, 3:08 am, Nathan E. \<rails-mailing-l…@andreas-
