所以我使用祖先 gem 来嵌套评论,一切正常,回复、编辑、创建、删除。但由于某种原因,他们自己的评论不会嵌套在您正在回复的评论下 - 相反,它最终会出现在评论列表的末尾。
代码
注意:任务有很多类别,其中类别属于任务
分类新方法
def new
@task = Task.find(params[:task_id])
@task.comments.new(:parent_id => params[:parent_id])
end
评论助手
module CommentsHelper
def nested_comments(comments)
comments.map do |comment, sub_comments|
render(comment) + content_tag(:div, nested_comments(sub_comments), :class => 'nested_comments')
end.join.html_safe
end
end
意见表
<%= form_for ([@task, @task.comments.build]) do |f| %>
<%= f.text_area :comment %>
<%= f.hidden_field :parent_id%>
<%= f.submit %>
<% end %>
当我回复或发布新评论时,这就是我在来源中得到的:
<div class="comment_23">
<p>I am a comment</p>
<div class="comment Info">
Written by: Adam |
You can: <a href="/tasks/7/comments/new?parent_id=23">Reply To Comment</a> or
<a href="/tasks/1/comments/23/edit">Edit Comment</a> |
<a href="/tasks/1/comments/23" data-method="delete" rel="nofollow">Delete Comment</a>
</div>
</div>
<div class="nested_comments"></div><div class="comment_24">
<p>I am another comment</p>
<div class="comment Info">
Written by: Adam |
You can: <a href="/tasks/7/comments/new?parent_id=24">Reply To Comment</a> or
<a href="/tasks/1/comments/24/edit">Edit Comment</a> |
<a href="/tasks/1/comments/24" data-method="delete" rel="nofollow">Delete Comment</a>
</div>
</div>
<div class="nested_comments"></div><div class="comment_25">
<p>I am a reply to the first comment</p>
<div class="comment Info">
Written by: Adam |
You can: <a href="/tasks/7/comments/new?parent_id=25">Reply To Comment</a> or
<a href="/tasks/1/comments/25/edit">Edit Comment</a> |
<a href="/tasks/1/comments/25" data-method="delete" rel="nofollow">Delete Comment</a>
</div>
</div>
<div class="nested_comments"></div>
为什么有空的nested_comments div?而我是一个测试是对我是一个评论的回复,那么为什么它不在嵌套评论 div 中呢?