views:

25

answers:

1

As the title says, one of my helper functions to render partials isn't 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:

#<ContactGroup:0x103090c78>#<ContactGroup:0x103090b60>

I commented out the block function completely and it still displayed the above.

Could someone please give some insight on what I'm doing wrong?

Edit: I just realized that the ## is coming from @contact_groups being the last value assigned on the page. It's returning the value and not rendering any of the code within the block.

+1  A: 

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.

Peter
Thanks so much, worked great. But, I would like to point out that in this scope, render_to_string doesn't work -- You still have to use render.
treefrog
ah, great, thanks @rofly. have amended my answer.
Peter