繁体   English   中英

使用Rails中的祖先宝石回复评论不起作用

[英]Reply a comment not working using ancestry gem in Rails

我正在尝试使用Rails 5.1.6创建“ 从帖子回复评论系统

我正在使用祖先宝石

我遵循Railscasts#262作为指南。

我对此有疑问

当我对每个评论提交答复时,它变成了新评论。 没有回复评论。

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

<% form_for @message do|f| %> <% form_for @message do|f| %> (在我的情况下是@comment),

但是我得到了这个错误未定义的方法'comments_path'

所以,我这些代码:

<%= 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 %>

但是所有这些代码生成了一个新的Comment,而不是一个答复。

这是我的另一个代码: 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

发布模型

has_many :comments

评论模型

 has_ancestry
  belongs_to :post

查看/帖子/显示

视图/评论/ _评论

<%= 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM