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?
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 …
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.