0
、私はこのエラーを取得:ターミナルではActiveRecord :: InvalidForeignKeyがエラー
ActiveRecord::InvalidForeignKey in InventoriesController#destroy
SQLite3::ConstraintException: FOREIGN KEY constraint failed: DELETE FROM "inventories" WHERE "inventories"."id" = ?
を、それが
{"_method"=>"delete", "authenticity_token"=>"dBNU2GkV0+rOcp4NVEljm4oIpkdOnPsvZKdmisaadBzX3QkY1VwurZNRPL0WFtVvizeAcJb7H6E50ObmpRsXAg==", "id"=>"1"}
を言うことも原因であると言います時:私の在庫で
def destroy
@inventory = Inventory.find(params[:id])
@inventory.destroy
redirect_to inventory_path
end
あるファイル:
class InventoriesController < ApplicationController
def show
@inventory = Inventory.find(params[:id])
end
def index
@inventories = Inventory.all
end
def new
@inventory = Inventory.new
end
def create
@inventory = Inventory.new(inventory_params)
if @inventory.save
redirect_to @inventory
else
render 'new'
end
end
def edit
@inventory = Inventory.find(params[:id])
end
def update
@inventory = Inventory.find(params[:id])
if @inventory.update(inventory_params)
redirect_to @inventory
else
render 'edit'
end
end
def destroy
@inventory = Inventory.find(params[:id])
@inventory.destroy
redirect_to inventory_path
end
end
private
def inventory_params
params.require(:inventory).permit(:product_name, :brand_name, :item_id, :upc_code, :color, :department, :size, :condition, :fabric_type, :shipping_weight, :sku, :asin, :quantity, :cost_price, :sell_price, :key_product_features, :product_description, :search_terms, :status, :listing_in_usa, :listing_in_canada, :listing_in_mexico)
end
あなたは 'Inventory'と' Item'の間に関係があるので、それに対処したり、あなたの関係の中に 'destroy''依存関係を指定する必要があります。 –