[エラー内容]
paramsが空だと言ってますがbinding.pryで確認したところ
14: def create 15: binding.pry => 16: item = Item.find(params[:item_id]) 17: cart_item = current_end_user.cart_items.new(cart_item_params) 18: cart_item.item_id = item.id 19: if cart_item.save 20: redirect_to cart_items_path 21: else render 'public/items/show' 22: end 23: end [1] pry(#<Public::CartItemsController>)> params => <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"oLYZB8MASqO0G7DzX4zNXj8sDCGKAtdjrPkhmK8uk5q9TCvXC1/r2YAkZMjCxmylXJ0J3CbuQg9u/nZHQD9dhg==", "quantity"=>"1", "commit"=>"カートに入れる", "controller"=>"public/cart_items", "action"=>"create", "item_id"=>"1"} permitted: false>
このようにparamsでデータは取得できている状態です。なのでなぜエラーになるのかがわかりません。
controllers/public::cart_items_controller
class Public::CartItemsController < ApplicationController def index end def update end def destroy end def destroy_all end def create item = Item.find(params[:item_id]) cart_item = current_end_user.cart_items.new(cart_item_params) binding.pry cart_item.item_id = item.id if cart_item.save redirect_to cart_items_path else render 'public/items/show' end end -エラー箇所- private def cart_item_params params.require(:cart_item).permit(:quantity) end end
controllers/public::items_controller
class Public::ItemsController < ApplicationController def top end def index @items = Item.all end def show @item = Item.find(params[:id]) @cart_item = CartItem.new end end
show.html.erb-items
<div class="row"> <div class="col-xs-2"></div> <div class="col-xs-10"> <div class="col-xs-5"> <%= attachment_image_tag @item,:image,format:'jpeg',fallback:'no_image.jpg',size:'150x150'%> </div> <div class="col-xs-7"> <h3><%= @item.name %></h3> <p><%= @item.detail %></p> <h3>¥<%= "#{@item.price * 1.10}".to_i %> <p style="display: inline-block; font-size: 15px">(税込)</p> </h3> <%= form_with model:@cart_item,url:item_cart_items_path(@item.id),id:'cart_items',class: 'cart_items',local: true do |f| %> <%= f.select :quantity,[["1",1],["2",2],["3",3],["4",4],["5",5],["6",6],["7",7],["8",8],["9",9],["10",10]],include_blank: "個数選択" %> <%= f.submit "カートに入れる",class:'btn btn-primary' %> <% end %> </div> </div> </div>
routes.rb
scope module: :public do resources :items,only: [:index,:show] resource :end_users,only: [:show,:edit,:update] get 'end_users/leave' => 'end_users#leave' patch 'end_users/withdrawal' => 'end_users#withdrawal' resources :items,only: [] do resources :cart_items,only: [:create] end resources :cart_items,only: [:index,:update,:destroy] delete 'cart_items' => 'cart_items#destroy_all' end
よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。