Reply post comment using Nested Comment from Ancestry gem rails

387 Views Asked by At

Im creating nested comment in my post using ancestry gem.

And i use Rails 5.

Here's my codes :

Post model

has_many :comments

Comment model

  has_ancestry
  belongs_to :post

I render all comments inside views/posts/show

<%= render 'comment' %>

Inside views/comments/_comments

<%= div_for(comment) do %>
  <%= comment.body %>
  <%= link_to "Reply", new_post_comment_path(:parent_id => comment, :post_id => @post.id) %>
<% 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

comment form

<%= simple_form_for new_post_comment_path(@post,@comment)  do |f| %>
    <%= f.hidden_field :parent_id %>
    <%= f.text_area :body, class: "form-control" %>
    <%= f.submit "Leave Comment", class: 'btn btn-main btn-block'  %>
<% end %>

Routes.rb

resources :posts do
    resources :comments
end

rake routes

           post_comments GET      /:post_id/comments(.:format)            comments#index
                         POST     /:post_id/comments(.:format)            comments#create
        new_post_comment GET      /:post_id/comments/new(.:format)        comments#new
       edit_post_comment GET      /:post_id/comments/:id/edit(.:format)   comments#edit
            post_comment GET      /:post_id/comments/:id(.:format)        comments#show
                         PATCH    /:post_id/comments/:id(.:format)        comments#update
                         PUT      /:post_id/comments/:id(.:format)        comments#update
                         DELETE   /:post_id/comments/:id(.:format)        comments#destroy

In this URL http://localhost:3000/post_id/comments/new?parent_id=10 , i clicked "Reply Comment", it rendered new comment form. But if i click submit button, i got this error

No route matches [POST] "/post_id/comments/new"

Please someone help me to solve it. Thank You

1

There are 1 best solutions below

2
Denny Mueller On

Forget my comment. I mixed something up.

Your issue is at

comment, :post_id => @post.id) %>

new_post_comment_path resolves to /:post_id/comments/new(.:format) comments#new which doesn't work since its a GET path and doesn't have a POST. You have to post to /:post_id/comments(.:format)

Since I can try it out locally now you could try

<%= link_to "Reply", post_comment_path(:parent_id => comment, :post_id => @post.id), method: :post %>

or manually add the correct path like

link_to 'foo', :action => :foo, :id => @bar.id, method: :post

or

link_to 'foo', '/foo/#{@bar.id}', method: :post