STI, controllers and views

Ryan Glover wrote:


I have set up that uses single table inheritance. I have a number of
model subclasses following the example in AWDWR that uses Manager <
Employee < Person.

First question. Do I need to create controller classes for each? I
don't think I do because a single controller should be able to shunt the
data between the models and the views.

Second question. Say I have a view that shows a list of all people in
the db. It shows first name, last name, and a 'show' link to view that
persons details. For each type I would like to show a specific view,
i.e. the manager_view, the employee_view etc. If I have a single
controller with a show action and I pass it the person as a parameter
how do I get the controller to show the correct view?

Thank you,
Ryan Glover

First question: no, not necessary to have separate controllers. Using some convention of config naming (see below) it should work out nicely using the fact that @someinstance.class will return the STI subclass and @someinstance.class.base_class will return the, er, base class

Second question: You can do something in your controller like:
def show
    @item = Person.find(params[:id]) # where Person is your base class
    render :template => "/people/#{@item.class.base_class}_view" # assuming the controller is called people and the templates employee_view, manager_view

Alternatively if there's a lot of common stuff in the show templates, just have one show template and use partials in the view to show the bit that's unique to the STI subclasses.