在 Rails 中回复使用祖先 gem 无效的评论

Reply a comment not working using ancestry gem in Rails

我正在尝试使用 Rails 5.1.6[=74 创建“从 Post 回复评论系统” =]

我正在使用 Ancestry Gem

我以 Railscasts #262 为指导。

我遇到了问题,

When I submit a reply to each comment, it became a new comment. Not a reply of comment.

我在他的表单中做了 railscast 所做的事情

<% form_for @message do|f| %>(在我的例子中是@comment),

但是我得到了这个错误 undefined method `comments_path' for

所以,我这些代码:

<%= simple_form_for [@post, @comment]  do |f| %>
  <%= f.hidden_field :parent_id %>
  <%= f.text_area :body %>
<% end %>

<%= simple_form_for [@post, Comment.new]  do |f| %>
  <%= f.hidden_field :parent_id %>
  <%= f.text_area :body %>
<% end %>

<%= simple_form_for [:post, Comment.new]  do |f| %>
  <%= f.hidden_field :parent_id %>
  <%= f.text_area :body %>
<% end %>

但是所有这些代码都生成了新评论,而不是回复。

这是我的另一个代码: comments_controller

def create
    @comment = @post.comments.new(comment_params)
    @comment.user = current_user

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @post, notice: 'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  def new
    @comment = Comment.new(:parent_id => params[:parent_id])
  end

Post 型号

has_many :comments

评论模型

 has_ancestry
  belongs_to :post

views/posts/show

views/comment/_comment

<%= div_for(comment) do %>
  <%= comment.body %>
  <%= link_to "Reply", new_post_comment_path(:parent_id => comment, :post_id => @post) %>
<% end %>

路线

resources :posts do
    resources :comments, only: [:destroy, :new, :create]
end

耙子路线

post_comments POST     /:post_id/comments(.:format)            comments#create
        new_post_comment GET      /:post_id/comments/new(.:format)        comments#new
            post_comment DELETE   /:post_id/comments/:id(.:format)        comments#destroy

有没有人可以帮助我?

终于找到答案了..

我把:parent_id放在comment_params

里面
  def comment_params
    params.require(:comment).permit(:post_id, :body, :user_id, :parent_id)
  end