Ruby on rails Can';t获取嵌套注释以处理祖先宝石

Ruby on rails Can';t获取嵌套注释以处理祖先宝石,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我正在尝试在rails中实现对我的评论的回复。我在看这段视频作为指导 我创建的所有注释都会成为根节点,每次我尝试回复注释时,它似乎没有注册为子节点。 (我已经通过rails控制台检查过,回复注释的“祖先”列始终为零) 我的评论是Post模型下的嵌套资源。我怀疑问题在于comments控制器中的create函数 使用rails“4.2.4” 财务主任评论: def new @post = Post.find(params[:post_id]) @comment = @post.c

我正在尝试在rails中实现对我的评论的回复。我在看这段视频作为指导 我创建的所有注释都会成为根节点,每次我尝试回复注释时,它似乎没有注册为子节点。 (我已经通过rails控制台检查过,回复注释的“祖先”列始终为零)

我的评论是Post模型下的嵌套资源。我怀疑问题在于comments控制器中的create函数

使用rails“4.2.4”

财务主任评论:

def new
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.build
    @comment.parent_id = params[:parent_id]

end

def create

    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(comment_params)
    @comment.user = current_user

    if @comment.save
        redirect_to build_post_path(@post)
    else
        redirect_to build_post_path(@post)
    end
end
comments/new.html.erb

    <div class = "row">
        <div class="col-md-8 col-md-offset-2 ">
            <h3>Reply</h3>
            <ol class ="comments">
                <%= render @comment.parent if @comment.parent %>
            </ol>
        </div>
    </div>


    <%= render 'form' %>
<div class = "row">
    <div class="col-md-8 col-md-offset-2 ">
        <h3>Comments (<%= @post.comments.count %>)</h3>
        <ol class ="comments">
            <%= nested_messages @post.comments.arrange(:order => :created_at) %>
        </ol>
    </div>
</div>

您正在表单声明中调用Comment.new,尽管您已经在新方法中启动了新的注释,请将表单调用更改为:

<%= form_for([@post, @comment]) do |f| %>


我认为通过添加,您不必手动设置父注释的值。

常见的陷阱。。。您是否在注释参数中包含了该参数?当您单击“回复”时,还要检查隐藏的父注释id表单是否具有父注释的值!感谢您的第一个建议,我没有将父id添加到评论参数中!但是,隐藏的父\u id值没有传递给create函数。我之所以知道这一点,是因为我是手动输入的,嵌套注释起作用了!我不知道为什么它没有通过…我已经修好了!我做了@comment.parent_id%>非常感谢您不知道我在这方面花了多长时间,对Railse来说还是很新的请查看我的代码示例作为答案,我认为您可以在不添加此内容的情况下实现这一点!我以前尝试过,但它不起作用,因为我没有在参数中包含父id,但现在我已经包含了,我可以再次使用它了!谢谢
<div class = "row">
    <div class="col-md-8 col-md-offset-2 ">
        <h3>Comments (<%= @post.comments.count %>)</h3>
        <ol class ="comments">
            <%= nested_messages @post.comments.arrange(:order => :created_at) %>
        </ol>
    </div>
</div>
def nested_messages(messages)
    messages.map do |message, sub_messages|
      render(message) + content_tag(:div, nested_messages(sub_messages), :class => "nested_messages")
    end.join.html_safe
  end
<%= form_for([@post, @comment]) do |f| %>