Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 743
kdweber89 Avatar asked May 07 '26 02:05

kdweber89


1 Answers

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.

like image 98
Tobias Avatar answered May 09 '26 01:05

Tobias