NoMethodError in Customers::OrdersController#create
undefined method `cart_items' for #Customer:0x00000000043c4f48 Did you mean? cart_ids
@cart_items = current_customer.cart_items
上記のエラーが発生しております。@cart_items = current_customer.cart_itemsの部分でエラーが発生していると思い、コントローラーを確認したのですが、何が間違っているのかが分かりません。Rails初心者です。よろしくお願いします。
エラー画面
NoMethodError in Customers::OrdersController#create undefined method `cart_items' for #<Customer:0x00000000043c4f48> Did you mean? cart_ids
view画面
<%= form_with model: @order, url: orders_path, method: :post, local: true do |f| %> <%= f.hidden_field :total_price, value: @total %> <%= f.submit "注文を確定する" %> <% end %>
Orderコントローラー
class Customers::OrdersController < ApplicationController def new @order = Order.new @cart_item = CartItem.where(customer_id: current_customer.id) @customer = current_customer end def create @cart_items = current_customer.cart_items @order = current_customer.order.new(order_params) if order.save @cart_items.each do |cart_item| order_item = OrderItem.new order_item.item_id = cart_item.item_id order_item.order_id = @order.id order_item.tax_price = cart_item.item.price order_item.number_of_piaces = cart_item.pieces order_item.save end redirect_to orders_complete_orders_path @cart_items.destroy.all else @order = Order.new(order_params) render.new end end def index @orders = current_customer.orders end def show @order = Order.find(params[:id]) @ordered_items = @order.order_items @cart_items = current_customer.cart_items.all end def complete end private def order_params params.require(:order).permit(:customer_id,:logged_out_on, :total_price, :payment_method, :receiver_name, :shipping_postal_code, :delivery_address, :order_status) end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/12/09 06:56