在Rails 3中递归渲染集合

zis*_*she 5 collections rendering partial-views ruby-on-rails-3

我想显示评论树.我在另一个视图中移动了注释div,并写下了下一行_comments.html.haml:

= render :partial => 'single_comment', :collection => @post.comments.where(:parent_id => nil)
Run Code Online (Sandbox Code Playgroud)

_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)
Run Code Online (Sandbox Code Playgroud)

但浏览器显示错误:

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
Run Code Online (Sandbox Code Playgroud)

我试图添加:as => comment第一行,但它不起作用.所以作为@comment部分使用.也许这根本不对?

Dra*_*ken 8

您必须:as => :comment在两个渲染线上添加,请记住正在渲染的答案再次呈现相同的部分,因此他们也会尝试渲染答案.

尝试添加:as => :comment注释和答案渲染部分.