#前提・実現したいこと
チーム開発をしていて、商品を削除する実装をしています。
mysqlに直接データを入力しましたのでデータはいくつか入っています。
データはあるのになぜレコードがないというエラーが出るのでしょうか?
検索しても直し方が分からず解決できませんでした。
わかる方がいらっしゃいましたら教えてくださると助かります。
#発生している問題・エラーメッセージ
ActiveRecord::RecordNotFound in ItemsController#show Couldn't find Item with 'id'=destroy Extracted source (around line #102): def get_item @item = Item.find(params[:id]) end end Application Trace | Framework Trace | Full Trace app/controllers/items_controller.rb:102:in `get_item' Request Parameters: {"id"=>"destroy"} Toggle session dump Toggle env dump Response Headers: None
#該当のソースコード
controller
1class ItemsController < ApplicationController 2 before_action :authenticate_user!, except: [:index, :show] 3 4 before_action :get_categories 5 6 before_action :get_item, except: [:index, :new, :create] 7 8 def index 9 @items = Item.includes(:images).order('created_at DESC').limit(5) 10 end 11 12 def show 13 end 14 15 16 def new 17 @category_array = [] 18 @categories.each do |category| 19 category.children.each do |child| 20 child.children.each do |gchild| 21 @category_array << [category.name, category.id] 22 @category_array << [child.name, child.id] 23 @category_array << [gchild.name, gchild.id] 24 end 25 end 26 end 27 @item = Item.new 28 @item.images.new 29 end 30 31 def create 32 @item = Item.new(item_params) 33 if @item.save 34 redirect_to root_path 35 else 36 render :new 37 end 38 end 39 40 def edit 41 end 42 43 def update 44 if @item.update(item_params) 45 redirect_to root_path 46 else 47 render :edit 48 end 49 end 50 51 def destroy 52 if @item.destroy 53 redirect_to root_path 54 else 55 render :edit 56 end 57 end 58 59 def confirmation 60 @user = User.find(current_user.id) 61 end 62 require "payjp" 63 def pay 64 if @item.stage != "selling" 65 redirect_to item_path(params[:id]) 66 else 67 @item.with_lock do 68 if current_user.credit_card.present? 69 @card = CreditCard.find_by(user_id: current_user.id) 70 Payjp.api_key = Rails.application.credentials.dig(:payjp, :PAYJP_SECRET_KEY) 71 charge = Payjp::Charge.create( 72 amount: @item.price, 73 customer: Payjp::Customer.retrieve(@card.customer_id), 74 currency: 'jpy' 75 ) 76 else 77 Payjp::Charge.create( 78 amount: @item.price, 79 card: params['payjp-token'], 80 currency: 'jpy' 81 ) 82 end 83 @purchase = Purchase.create(user_id: current_user.id, item_id: params[:id]) 84 item = Item.find(params[:id]) 85 item.update(stage: 1) 86 redirect_to root_path 87 end 88 end 89 end 90 91 private 92 def get_categories 93 @categories = Category.where(ancestry: nil) 94 end 95 96 def item_params 97 params.require(:item).permit(:name, :price, :size, :condition, :brand, :stage, :detail, :category_id, :prefecture, :fee, :delivery_date, images_attributes: [:image, :destroy, :id]).merge(user_id: current_user.id) 98 end 99 100 def get_item 101 @item = Item.find(params[:id]) 102 end 103end 104
.Top .Top__header %a{href: "#"} %img{alt: "FURIMA", src: "/assets/logo-9486ef715c62ec78e487e9fd713395dee60de0966916f621cbc19d0462dbf03b.png", :width: "400", height: "100"}/ .success %h2 削除が完了しました %a.btn{:href: "/"} トップへ戻る
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/18 02:58
2020/08/18 03:09
2020/08/18 03:12
2020/08/18 03:14
2020/08/18 03:16