# STI, controllers and views

**URL:** <https://www.ruby-forum.com/t/sti-controllers-and-views/80588>\
**Category:** Rails\
**Created:** [December 28, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/sti-controllers-and-views/80588 "2006-12-28T22:48:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [December 28, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/sti-controllers-and-views/80588/1 "2006-12-28T22:48:58Z")

</div>

Hello,

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 G.

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [December 29, 2006, 2:24am UTC](https://www.ruby-forum.com/t/sti-controllers-and-views/80588/2 "2006-12-29T02:24:26Z")

</div>

Ryan G. wrote:

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

No, you can use one controller if it makes sense in your application.

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

Lots of ways. The first that comes to my mind would be …

render :template =\> “people/#{@person.type.to\_s.underscore}”

(assuming /app/views/people/manager.rhtml, etc.)  
but I’m sure someone will come up with an option in even fewer lines of  
code. 😉

---

<div class="post-metadata">

**Author:** ![wonko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8dc957/32.png) [@wonko](https://www.ruby-forum.com/u/wonko)\
**Post date:** [December 29, 2006, 2:40am UTC](https://www.ruby-forum.com/t/sti-controllers-and-views/80588/3 "2006-12-29T02:40:27Z")

</div>

Ryan G. wrote:

> 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 G.

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  
end

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.

HTH

Chris
