前提・実現したいこと
現在railsでECサイトを作成しております。
カート機能を実装しましたが、カートに移動しようとすると、以下のエラーが発生致します。
発生している問題・エラーメッセージ
ActiveRecord::RecordNotFound at /line_items/2 Couldn't find LineItem with 'id'=2
models/concerns/current_cart.rb
Ruby
1module CurrentCart 2 private 3 def set_cart 4 @cart = Cart.find(session[:cart_id]) 5 rescue ActiveRecord::RecoedNotFound 6 @cart = Cart.create 7 session[:cart_id] = @cart.id 8 end 9end 10
controllers/line_items_controller.rb
Ruby
1class LineItemsController < ApplicationController 2 include CurrentCart 3 before_action :set_line_item, only: [:show, :edit, :update, :destroy] 4 before_action :set_cart, only: [:create] 5 6 7 def index 8 @line_items = LineItem.all 9 end 10 11 def show 12 end 13 14 def new 15 @line_item = LineItem.new 16 end 17 18 def edit 19 end 20 21 def create 22 item = Item.find(params[:item_id]) 23 @line_item = @cart.add_item(item) 24 25 respond_to do |format| 26 if @line_item.save 27 format.html { redirect_to @line_item.cart, notice: 'カゴに追加されました' } 28 format.json { render :show, status: :created, location: @line_item } 29 else 30 format.html { render :new } 31 format.json { render json: @line_item.errors, status: :unprocessable_entity } 32 end 33 end 34 end 35 36 def update 37 respond_to do |format| 38 if @line_item.update(line_item_params) 39 format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' } 40 format.json { render :show, status: :ok, location: @line_item } 41 else 42 format.html { render :edit } 43 format.json { render json: @line_item.errors, status: :unprocessable_entity } 44 end 45 end 46 end 47 48 49 def destroy 50 @cart = Cart.find(session[:cart_id]) 51 @line_item.destroy 52 respond_to do |format| 53 format.html { redirect_to cart_path(@cart), notice: 'Line item was successfully destroyed.' } 54 format.json { head :no_content } 55 end 56 end 57 58 private 59 def set_line_item 60 @line_item = LineItem.find(params[:id]) 61 end 62 63 def line_item_params 64 params.require(:line_item).permit(:item_id) 65 end 66end 67
controllers/item_controller.rb
Ruby
1class ItemsController < ApplicationController 2 before_action :set_item, only: [:show, :edit, :update, :destroy] 3 before_action :authenticate_user!, except: [:index, :show] 4 5 def index 6 @items = Item.all.order("created_at desc") 7 end 8 9 def show 10 end 11 12 def new 13 @item = current_user.items.build 14 end 15 16 def edit 17 end 18 19 20 def create 21 @item = current_user.items.build(item_params) 22 23 respond_to do |format| 24 if @item.save 25 format.html { redirect_to @item, notice: 'Item was successfully created.' } 26 format.json { render :show, status: :created, location: @item } 27 else 28 format.html { render :new } 29 format.json { render json: @item.errors, status: :unprocessable_entity } 30 end 31 end 32 end 33 34 def update 35 respond_to do |format| 36 if @item.update(item_params) 37 format.html { redirect_to @item, notice: 'Item was successfully updated.' } 38 format.json { render :show, status: :ok, location: @item } 39 else 40 format.html { render :edit } 41 format.json { render json: @item.errors, status: :unprocessable_entity } 42 end 43 end 44 end 45 46 def destroy 47 @item.destroy 48 respond_to do |format| 49 format.html { redirect_to items_url, notice: 'Item was successfully destroyed.' } 50 format.json { head :no_content } 51 end 52 end 53 54 private 55 def set_item 56 @item = Item.find(params[:id]) 57 end 58 59 def item_params 60 params.require(:item).permit(:brand, :model, :description, :condition, :finish, :title, :price, :image) 61 end 62end 63
controllers/carts_controller.rb
Ruby
1class CartsController < ApplicationController 2 rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart 3 before_action :set_cart, only: [:show, :edit, :update, :destroy] 4 5 6 def index 7 @carts = Cart.all 8 end 9 10 def show 11 end 12 13 def new 14 @cart = Cart.new 15 end 16 17 def edit 18 end 19 20 def create 21 @cart = Cart.new(cart_params) 22 23 respond_to do |format| 24 if @cart.save 25 format.html { redirect_to @cart, notice: 'Cart was successfully created.' } 26 format.json { render :show, status: :created, location: @cart } 27 else 28 format.html { render :new } 29 format.json { render json: @cart.errors, status: :unprocessable_entity } 30 end 31 end 32 end 33 34 35 def update 36 respond_to do |format| 37 if @cart.update(cart_params) 38 format.html { redirect_to @cart, notice: 'Cart was successfully updated.' } 39 format.json { render :show, status: :ok, location: @cart } 40 else 41 format.html { render :edit } 42 format.json { render json: @cart.errors, status: :unprocessable_entity } 43 end 44 end 45 end 46 47 48 def destroy 49 @cart.destroy if @cart.id == session[:cart_id] 50 session[:cart_id] = nil 51 respond_to do |format| 52 format.html { redirect_to carts_url, notice: 'Cart was successfully destroyed.' } 53 format.json { head :no_content } 54 end 55 end 56 57 private 58 def set_cart 59 @cart = Cart.find(params[:id]) 60 end 61 62 def cart_params 63 params.fetch(:cart, {}) 64 end 65 def invalied_shopcart 66 logger.error "attemp to access invalid shopcart #{@params[:id]}" 67 redirect_to root_path, notice: "このカートは空です" 68 end 69end 70
routes.rb
Ruby
1Rails.application.routes.draw do 2 resources :line_items 3 resources :carts 4 resources :items 5 devise_for :users, controllers:{ 6 registrations: 'registrations' 7 } 8 root 'items#index' 9 devise_scope :user do 10 get '/users/sign_out' => 'devise/sessions#destroy' 11 end 12end 13
controllers_application_controller.rb
Ruby
1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 # 追加したが、変化なし 4 # include CurrentCart 5 # before_action :set_cart 6end
試したこと
application_controller.rbに
include CurrentCart
before_action :set_cart
を追加してみましたが、変化はありませんでした。
LineItemを判別できていないエラーだと認識しております。
また、binding.pryで検証したところ、@line_itemの中身はnilとなっておりました。
原因が見当もつかず、困っております。
冗長なコードではありますが、何卒お力添えの程宜しくお願い致します。
補足情報(FW/ツールのバージョンなど)
rails 6.0.0
ruby 2.5.1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。