在Rails 3中递归渲染集合

时间:2011-08-24 13:17:58

标签: ruby-on-rails-3 collections rendering partial-views

我想显示评论树。我在另一个视图中移动了注释div,并在_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)

但浏览器显示错误:

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

我尝试在第一行添加:as => comment,但它不起作用。所以在部分中使用@comment。 也许这根本就错了?

1 个答案:

答案 0 :(得分:8)

你必须在两个渲染线上添加:as => :comment,记住正在渲染的答案再次呈现同样的部分,所以他们也会尝试渲染答案。

尝试在评论和答案呈现部分添加:as => :comment