2

I have an STI model and would like to reuse the base views for the derived model. For example, If I have Teacher < Person I would like the Teacher's view to contain the person's view fields.

Is there a way to achieve this?

danivovich
  • 4,197
  • 4
  • 30
  • 31

3 Answers3

1

If I'm understanding your question...they're available by default. When using STI each derived model has access to all the fields from the base class. (any field on the table)

For example, when rendering a partial, you could simply pass the object as normal:

= render :partial => 'person', :object => @teacher

The code above can be done in several ways, but I'm just trying to illustrate.

Jimmy Baker
  • 3,215
  • 1
  • 24
  • 26
  • Is there a way to control the layout when using this approach? When doing this I get the edit/delete links at the end of each item and I don't get a chance to add the derived fields (assuming I don't want to put them at the beginning)? – user1080584 Mar 11 '12 at 06:02
  • Sure. There are a couple of different ways to do this. I'm not a huge fan of view logic, but you could have an if statement that checks the kind of person it is: "if person.kind_of?(Teacher)" – Jimmy Baker May 17 '12 at 15:47
0

If you are having teachers and persons controller then you can render views(partials, templates) from persons inside teachers views.

Examples

You can write below lines inside teachers views

= render :partial => 'persons/<PARTIAL NAME>'

= render :template => 'persons/<TEMPLATE>'

= render :file => 'persons/new'
Sandip Ransing
  • 7,583
  • 4
  • 37
  • 48
0

render use the right partial so if you have the _teacher.html.erb and _person.html.erb partials you can render the person partial inside the teacher one and when you call render with an object the right partial will be rendered:

# @aldo is a Person, this will render _person.html.erb
render @aldo

# @mr_brown si a Teacher, this will render _teacher.html.erb
render @mr_brown

Because the teacher partial render the person one you have in that case both the information.

Aldo 'xoen' Giambelluca
  • 12,075
  • 7
  • 33
  • 39
  • Is there a way to control the layout when using this approach? When doing this I get the edit/delete links at the end of each item and I don't get a chance to add the derived fields (assuming I don't want to put them at the beginning)? – user1080584 Mar 14 '12 at 11:42
  • Sorry I don't understand well what do you mean, do you have the two partials and render the person one in the teacher one, right? Are the person's field not shown? – Aldo 'xoen' Giambelluca Mar 14 '12 at 12:38