[英]Reply a comment not working using ancestry gem in Rails
I'm trying to create " Reply a Comment System from Post " using Rails 5.1.6 我正在尝试使用Rails 5.1.6创建“ 从帖子回复评论系统 ”
And I am using Ancestry Gem . 我正在使用祖先宝石 。
I followed Railscasts #262 as a guide. 我遵循Railscasts#262作为指南。
I got a problem with this, 我对此有疑问
When I submit a reply to each comment, it became a new comment. 当我对每个评论提交答复时,它变成了新评论。 Not a reply of comment. 没有回复评论。
I did what railscast
did inside his form 我做了railscast
在他的表格中所做的事情
<% form_for @message do|f| %>
<% form_for @message do|f| %>
(in my case is @comment), <% form_for @message do|f| %>
(在我的情况下是@comment),
but i got this error undefined method `comments_path' for 但是我得到了这个错误未定义的方法'comments_path'
So, i these codes : 所以,我这些代码:
<%= simple_form_for [@post, @comment] do |f| %>
<%= f.hidden_field :parent_id %>
<%= f.text_area :body %>
<% end %>
and 和
<%= simple_form_for [@post, Comment.new] do |f| %>
<%= f.hidden_field :parent_id %>
<%= f.text_area :body %>
<% end %>
and 和
<%= simple_form_for [:post, Comment.new] do |f| %>
<%= f.hidden_field :parent_id %>
<%= f.text_area :body %>
<% end %>
But all these codes generated a new Comment, not a reply. 但是所有这些代码生成了一个新的Comment,而不是一个答复。
Here's my another codes : comments_controller 这是我的另一个代码: 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 model 发布模型
has_many :comments
Comment model 评论模型
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 %>
Routes 路线
resources :posts do
resources :comments, only: [:destroy, :new, :create]
end
Rake Routes 耙道
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
Is there anyone who can help me Please? 请问有人可以帮助我吗?
Finally i found the answer.. 终于我找到了答案。
I put :parent_id inside comment_params 我把: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.