Suppose I have a model Person which has a one to many relationship with itself (i.e. Parent -> Child). When show.html.erb
is rendered on a given @person
I would like it to also include the result of rendering of show.html.erb
for the parent.
Essentially I would like the correct syntax for
<%= render @person.person %>
I actually tried manually doing
<%= render :file => "/persons/1.html.erb" %>
and it didn't work. Can someone help me understand the syntax of render
? Thanks.
(Note that I actually have a more c开发者_如何学Pythonomplicated model and I am rendering SVG files, not html.)
Put most of the content of show.html.erb in a file called _person.html.erb and then in show.html.erb have this:
<%= render :partial => 'person', :locals => {:person => @person} %>
Use person instead of @person in _person.html.erb
(Note that the file name starts with an underscore.)
Create a method in Person model to fetch an array of related people, e.g. parents:
def parents(person)
Person.find(:all, :conditions => ...
end
In controller, fetch the parents:
@parents = Person.parents(someone)
In person view (show.html.erb
), loops the parents array and pass each to the partial view as a parameter:
<% @parents.each do |parent| %>
<%= render :partial => 'info', :locals => { :person => parent } %>
<% end %>
Create a partial view: _info.html.erb
, the person
is a local variable storing a person record, and can be access within the partial form.
精彩评论