kdweber89
kdweber89

Reputation: 2174

Delete not working in crud functionality (rails)

I am having something is not working within my controller in that the specific items that I have in my app are not deleting when the button is being hit. I've tried adding a binding.pry right at the beginning of my destroy method, and its not getting hit. As a result i'm not sure what I am doing wrong that is keeping my items from being deleted without using the rails console.

Would anybody know what I'm doing wrong?

My controller

class ItemsController < ApplicationController
before_action :find_item, only: [:show, :edit, :update, :destroy]

def destroy
   @item.destroy
   redirect_to root_path
 end

end

My show page

%h1= @item.title
%h3= @item.description

= link_to "Home", root_path
= link_to "Edit", edit_item_path(@item)
= link_to "Delete", item_path(@item), method: :delete, data: {confirm: "Are you sure?"}

My routes page is like:

Rails.application.routes.draw do
 resources :items
 root 'items#index'
end

My application.js file I have this

//= require jquery
//= require bootstrap-sprockets
//= require jquery_ujs
//= require turbolinks
//= require_tree

Upvotes: 3

Views: 823

Answers (2)

Tobias
Tobias

Reputation: 4653

I don't see any delete queries in your destroy action.

Try:

def destroy
  if @item.destroy
    redirect_to root_path
  else
    # Unable to delete ...
  end
end

Now the item should be deleted from the database and the user redirected after that.

Upvotes: 2

Zajn
Zajn

Reputation: 4088

What does your routes file look like?

Do you have a route for a delete method?

What actually happens when you click the link that you created? What is logged to the console?

Also, you're not actually deleting anything as part of your destroy action. You're only redirecting to the root_path.

Upvotes: 1

Related Questions