Pratyush
Pratyush

Reputation: 71

Rails 'link_to' giving an incorrect path to delete a Nested Resource

I made a simple blog application in rails and when I tried giving a link to delete a Comment rails gave me incorrect link

<a data-method="delete" href="/forums/27/comment.7" rel="nofollow">Delete</a>

This gave me an error

No route matches [DELETE] "/forums/27/comment.7"

but when I changed it to

<a data-method="delete" href="/forums/27/**comments/7**" rel="nofollow">Delete</a>

by chrome's Inspect it deleted my comment. So how to generate a correct Link ?

Controller

def destroy
  @comment.destroy
  redirect_to :back
end

private

  def find_forum 
    @forum = Forum.find(params[:forum_id])
  end

  def set_comment
    @comment = Comment.find(params[:id])
  end

show.html.haml

- @forum.comments.each do |c|
  = c.user.firstname
  = c.comment
  - if user_signed_in?
    - if c.user == current_user
      = link_to "Delete", [@forum, c], method: :delete

Routes

                  Prefix Verb   URI Pattern                                   Controller#Action
              forum_like PUT    /forums/:forum_id/like(.:format)              forums#like
            forum_unlike PUT    /forums/:forum_id/unlike(.:format)            forums#unlike
           forum_comment PUT    /forums/:forum_id/comment(.:format)           forums#comment
          forum_comments GET    /forums/:forum_id/comments(.:format)          comments#index
                         POST   /forums/:forum_id/comments(.:format)          comments#create
       new_forum_comment GET    /forums/:forum_id/comments/new(.:format)      comments#new
      edit_forum_comment GET    /forums/:forum_id/comments/:id/edit(.:format) comments#edit
                         GET    /forums/:forum_id/comments/:id(.:format)      comments#show
                         PATCH  /forums/:forum_id/comments/:id(.:format)      comments#update
                         PUT    /forums/:forum_id/comments/:id(.:format)      comments#update
                         DELETE /forums/:forum_id/comments/:id(.:format)      comments#destroy
                  forums GET    /forums(.:format)                             forums#index
                         POST   /forums(.:format)                             forums#create
               new_forum GET    /forums/new(.:format)                         forums#new
              edit_forum GET    /forums/:id/edit(.:format)                    forums#edit
                   forum GET    /forums/:id(.:format)                         forums#show
                         PATCH  /forums/:id(.:format)                         forums#update
                         PUT    /forums/:id(.:format)                         forums#update
                         DELETE /forums/:id(.:format)                         forums#destroy
        new_user_session GET    /users/sign_in(.:format)                      devise/sessions#new
            user_session POST   /users/sign_in(.:format)                      devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                     devise/sessions#destroy
           user_password POST   /users/password(.:format)                     devise/passwords#create
       new_user_password GET    /users/password/new(.:format)                 devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                devise/passwords#edit
                         PATCH  /users/password(.:format)                     devise/passwords#update
                         PUT    /users/password(.:format)                     devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)                       registrations#cancel
       user_registration POST   /users(.:format)                              registrations#create
   new_user_registration GET    /users/sign_up(.:format)                      registrations#new
  edit_user_registration GET    /users/edit(.:format)                         registrations#edit
                         PATCH  /users(.:format)                              registrations#update
                         PUT    /users(.:format)                              registrations#update
                         DELETE /users(.:format)                              registrations#destroy
                    root GET    /                                             forums#index

Upvotes: 0

Views: 688

Answers (1)

infused
infused

Reputation: 24357

You need to use the forum_comment_path helper:

= link_to "Delete", forum_comment_path(@forum, c), method: :delete

Upvotes: 1

Related Questions