Tôi muốn hiển thị cây nhận xét. Tôi di chuyển bình luận div theo quan điểm khác, và viết dòng tiếp theo trong _comments.html.haml
:Hiển thị đệ quy một bộ sưu tập trong Rails 3
= 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)
Nhưng trình duyệt chỉ cho tôi một lỗi:
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
Tôi cố gắng thêm :as => comment
trong dòng đầu tiên, nhưng nó không hoạt động. Vì vậy, khi sử dụng @comment
một phần. Có thể về cơ bản là sai?
Hoặc làm theo giải pháp @ Draiken hoặc thay đổi 'nhận xét' một phần thành 'single_comment' vì đó là biến được đặt tên theo quy ước. – rubish