カート機能を現在作成しております。
cartテーブルに外部キーとしてprotein_id,user_idを作成しており、またquatityカラムをcartテーブルに作成しています。
cartコントローラーにストロングパラメーターを記述したところエラーが出ました。
原因としては、protein.idが見つからないためエラーが出ていると予想しましたが解決方法が分かりません。
ご教授いただけば幸いです。
よろしくお願いします。
class CartsController < ApplicationController def index @cart = Cart.all end def new @protein = Protein.find(params[:format]) @cart = Cart.new end def create @cart = Cart.new(cart_params) if @cart.save redirect_to carts_path else render :new end end def destroy @cart.destroy redirect_to current_cart end private def cart_params params.require(:cart).permit(:quantity).merge(user_id: current_user.id, protein_id: @protein.id) end end
card.rb belongs_to :user has_many :proteins
protein.rb belongs_to :cart
def index @proteins = Protein.all end def new @protein = Protein.new end def create @protein = Protein.new(proteins_params) if @protein.save redirect_to root_path else render :new end end def show end def edit end def update if @protein.update(proteins_params) redirect_to protein_path else render :edit end end def destroy if @protein.destroy redirect_to proteins_path else render :show end end
protein show.hetml.erb <div class="cart"> <%=link_to "カートに追加", new_cart_path(@protein), class: 'cart-item' %> </div>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/20 14:49
2020/10/20 14:52
2020/10/20 23:30
2020/10/21 01:21
2020/10/21 03:53
2020/10/21 04:09
2020/10/21 04:39
2020/10/21 23:16