注释不使用祖先gem嵌套在Rails中

时间:2012-09-13 17:37:39

标签: ruby-on-rails ruby ruby-on-rails-3

所以我使用祖先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中?

0 个答案:

没有答案