I'm trying to create "Reply a Comment System from Post" using Rails 5.1.6
And I am using Ancestry Gem.
I followed Railscasts #262 as a guide.
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
<% form_for @message do|f| %>
(in my case is @comment),
but i got this error undefined method `comments_path' for
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.
Here's my another codes : 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
def comment_params
params.require(:comment).permit(:post_id, :body, :user_id, :parent_id)
end