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
= com开发者_开发百科ment.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?
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.
精彩评论