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

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

Lets say I have an application that has controller:

This controller would be accessable from the following url

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 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?


I'd extract the common actions in a module and include it in both

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

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' }

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

and then to extend:

class ChildrensController < ParentsController



I has the same issue when I wanted a SOAP controller
(ActionWebService) to reuse my Restful logic in the normal
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.


Check out RailsEngines: http://rails-engines.org/introduction

Have to agree on the RailsEngines.

If your views are non-changing then you may consider a combination of:

  • generator script to generate views and controllers

  • the controllers are actually ‘dumb’ ones that simply include a module(s)

  • the module(s) are in the plugin, including the Rails generators