Mark Hustad
Mark Hustad

Reputation: 169

Destroy path not working in comments but I can create a comment

I cannot figure this out. Everything seems to be set up correctly. The create comments is working fine but when I try to destroy it gives me a missing template error. I've searched and tried about everything I can think of. Part of me thinks its a JS issue but when I look at my logs it appears to work fine. I'm using devise as well FYI. Any help would be amazing. Thanks.

Error in browser

Missing template comments/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. 

_comment.html.erb

<p>

  <strong>Comment:</strong>
  <%= comment.body %>
  <p>posted by: <%= comment.user.name %></p>

</p>

  <p>
   <%= link_to 'Destroy Comment', [@pit, comment],
               method: :delete,
               data: { confirm: 'Are you sure?' } %>
</p>

Comments Controller

class CommentsController < ApplicationController


 def create
  @pit= Pit.find(params[:pit_id])
  @comment = @pit.comments.build(comments_params)
  @comment.user = current_user
  @comment.save

  redirect_to pit_path(@pit)
end

def destroy
    @pit = Pit.find(params[:pit_id])
    @comment = @pit.comments.find(params[:id])
    @comment.destroy
    redirect_to pit_path(@pit)
end

def show  
end

  private

def comments_params
    params.require(:comment).permit(:body, :user_id)
end

end

Pits Controller

def index
  @pit = Pit.all
  @user = User.find_by(params[:id])
  @pit = @user.pits
  @pits = Pit.order('created_at DESC').group_by { |pit| pit.created_at.strftime("%B %Y") }
end

def create
  @user = current_user
  @pit = current_user.pits.create(pit_params)
    if @pit.save
      redirect_to @pit
    else
      render 'new'
    end
end

def show
  @pit = Pit.find(params[:id])
end

def edit
end

def update
end


private

def pit_params
    params.require(:pit).permit(:topic, :summary, :image, :video_url, :author, :user_id)
end

end

Routes

Rails.application.routes.draw do

  devise_for :users, :controllers => { registrations: 'registrations' }
  devise_scope :user do
    get 'users/sign_in' => 'devise/sessions#new'
    get 'users/sign_out' => 'devise/sessions#destroy'
    match 'users/:id', to: 'users#show', as: 'user', via: 'get'
  end

  resources :pits do
    resources :comments
  end




  root to: 'pages#home'
  get '/about' => 'pages#about'
end

Routes

Prefix Verb   URI Pattern                               Controller#Action
        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
           users_sign_in GET    /users/sign_in(.:format)                  devise/sessions#new
          users_sign_out GET    /users/sign_out(.:format)                 devise/sessions#destroy
                    user GET    /users/:id(.:format)                      users#show
            pit_comments GET    /pits/:pit_id/comments(.:format)          comments#index
                         POST   /pits/:pit_id/comments(.:format)          comments#create
         new_pit_comment GET    /pits/:pit_id/comments/new(.:format)      comments#new
        edit_pit_comment GET    /pits/:pit_id/comments/:id/edit(.:format) comments#edit
             pit_comment GET    /pits/:pit_id/comments/:id(.:format)      comments#show
                         PATCH  /pits/:pit_id/comments/:id(.:format)      comments#update
                         PUT    /pits/:pit_id/comments/:id(.:format)      comments#update
                         DELETE /pits/:pit_id/comments/:id(.:format)      comments#destroy
                    pits GET    /pits(.:format)                           pits#index
                         POST   /pits(.:format)                           pits#create
                 new_pit GET    /pits/new(.:format)                       pits#new
                edit_pit GET    /pits/:id/edit(.:format)                  pits#edit
                     pit GET    /pits/:id(.:format)                       pits#show
                         PATCH  /pits/:id(.:format)                       pits#update
                         PUT    /pits/:id(.:format)                       pits#update
                         DELETE /pits/:id(.:format)                       pits#destroy
                    root GET    /                                         pages#home
                   about GET    /about(.:format)                          pages#about
                    page GET    /pages/*id 

Upvotes: 2

Views: 396

Answers (1)

Christian Rolle
Christian Rolle

Reputation: 1684

Since you are talking about "JS issue", I assume you want the destroy action to be called by an AJAX request. Since the nested route looks fine and you also assigned the correct HTTP verb, but you missed to assign remote: true to the link to execute an AJAX request, like:

    <%= link_to 'Destroy Comment', [@pit, comment],
           method: :delete,
           remote: true,
           data: { confirm: 'Are you sure?' } %>

Furthermore: The reason why you end up in show action, is the resource URL for both requests is the same (so your nested route works fine). But the HTTP verb seems to be missed.
Please do:
1.) post the generated HTML link
2.) post the request header (especially X-Requested-With and Accept)
3.) disable all custom JS except jQuery in your application.js (restart your webserver in RAILS_ENV=development) and test if the delete request still fails
If disabling custom JS in your application.js works for the link, the search, which custom JS make it fail...

Upvotes: 1

Related Questions