Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive rendering a collection in Rails 3

I want to show comments tree. I moved comment div in another view, and wrote next line in _comments.html.haml :

= render :partial => 'single_comment', :collection => @post.comments.where(:parent_id => nil)

_single_comments.html.haml:

- if comment.id != nil
  .comment
    .meta
      = comment.name
      says
    .body
      = comment.text
  .answers
    = render :partial => 'posts/single_comment', :collection => @post.comments.where(:parent_id => comment.id)

But browser show me an error:

undefined local variable or method `comment' for #<#<Class:0x00000004e39280>:0x00000004e2f398>
Extracted source (around line #1):

1: - if comment.id != nil
2:   .comment
3:     .meta
4:       = comment.name

I tried to add :as => comment in first line, but it doesn't work. So as a using @comment in partial. Maybe it's fundamentally wrong?

like image 523
zishe Avatar asked Aug 24 '11 13:08

zishe


1 Answers

You have to add :as => :comment on both render lines, remember the answers that are being rendered are rendering this same partial again, so they will try rendering answers too.

Try adding the :as => :comment on both the comments and the answers rendering part.

like image 180
Draiken Avatar answered Oct 13 '22 12:10

Draiken