プログラミング初心者です。
現在カート機能を実装しておりますが、cartの詳細ページには遷移でき、paramsも確認すると商品idとquantityの値は送られておりますが、中間テーブル(cart_item)にデータが保存されません。
ご教授頂ければ幸いです。
cart_item controller.rb before_action :set_cart_item, only: [:create, :destroy] before_action :set_user before_action :set_cart def create @cart_item = @cart.cart_items.build(protein_id: params[:protein][:protein_id], quantity: params[:cart_item][:quantity]) if @cart_item.save redirect_to current_cart else redirect_to protein_path end end def destroy @cart.destroy redirect_to current_cart end private def set_user @user = current_user end def set_cart_item @cart_item = current_cart.cart_items.find_by(protein_id: params[:protein_id]) end def set_cart @cart = current_cart end
cart_item.rb class CartItem < ApplicationRecord belongs_to :protein belongs_to :cart belongs_to :user end
cart.rb class Cart < ApplicationRecord has_many :cart_items end
protein.rb has_one_attached :image belongs_to :user has_one :buyer has_many :cart_items
carts controller.rb class CartsController < ApplicationController def show @protein = Protein.find(params[:id]) @cart_items = CartItem.all end end
あなたの回答
tips
プレビュー