删除未在crud功能(rails)中工作的内容



我的控制器中有一些东西不起作用,因为当按下按钮时,我应用程序中的特定项目没有删除。我试过在销毁方法的开头添加binding.pry,但它没有被击中。因此,我不确定我做错了什么,那就是在不使用rails控制台的情况下阻止我的项目被删除。

有人知道我做错了什么吗?

我的controller

class ItemsController < ApplicationController
before_action :find_item, only: [:show, :edit, :update, :destroy]
def destroy
   @item.destroy
   redirect_to root_path
 end
end

我的show

%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?"}

我的路线页面是这样的:

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

我的application.js文件我有这个

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

我在您的destroy操作中没有看到任何删除查询。

尝试:

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

现在,应该从数据库中删除该项目,然后重定向用户。

您的routes文件是什么样子的?

你有delete方法的路线吗?

当你点击你创建的链接时,实际会发生什么?控制台上记录了什么?

此外,作为销毁操作的一部分,您实际上并没有删除任何内容。您只是重定向到root_path

最新更新