Rails 3 中递归渲染集合
我想显示评论树。我将评论 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
。 也许从根本上来说这是错误的?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须添加
:as =>; :comment
在两条渲染线上,请记住正在渲染的答案将再次渲染相同的部分,因此它们也会尝试渲染答案。尝试添加
:as =>; :comment
评论和答案呈现部分。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.