One of my helper functions to render partials is not rendering correctly.
def render_contact_list
@contact_groups = ContactGroup.find(:all, :conditions => ["user_id = ?", current_user.id])
@contact_groups.each do |contact_group|
@contacts = Contact.find(:all, :conditions => ["group_id = ? and Owner_id = ?", contact_group.id, current_user.id])
render :text => "#{contact_group.name}<br />"
render(:partial => 'contacts/partials/view_contacts', :collection => @contacts, :as => :contact)
end
end
All that displays on the page is
##
When looking at the HTML of the rendered page, it looks like this:
开发者_Go百科#<ContactGroup:0x103090c78>#<ContactGroup:0x103090b60>
I commented out the block function completely and it still displayed the above.
What am I doing wrong?
Edit: I just realized that the ## is coming from @contact_groups being the last value assigned on the page. It is returning the value and not rendering any of the code within the block.
Your helper function is returning the list @contact_groups
. You need to return the partial instead. You might do it like this (rough example):
def render_contact_list
@contact_groups = ContactGroup.find(:all, :conditions => ["user_id = ?", current_user.id])
text = ""
@contact_groups.each do |contact_group|
@contacts = Contact.find(:all, :conditions => ["group_id = ? and Owner_id = ?", contact_group.id, current_user.id])
text << render :text => "#{contact_group.name}<br />"
text << render(:partial => 'contacts/partials/view_contacts', :collection => @contacts, :as => :contact)
end
text
end
Here, we build a string with all the partially-rendered pieces, then return that string (by mentioning it last). The fact that it worked with and without the block is a coincidence; both the first line of your function and the block evaluate to the same thing.
精彩评论