未定义的方法`prouct_price?'对于 class OrderItems--(OrderItemsController#create)

undefined method `prouct_price?' for class OrderItems--(OrderItemsController#create)

  CreateOrderItems Migration
  def change
    create_table :order_items do |t|
      t.integer :product_id
      t.integer :order_id
      t.decimal :unit_price
      t.integer :p_quantity
      t.decimal :total_price

      t.timestamps
    end
  end
 OrderItemsController
  def create
  @order = current_order
  @order_item =               @order.order_items.new(order_item_params)
  @order.save
  session[:order_id] = @order.id
 end
   def order_item_params
  params.require(:order_item).permit(:product_id, :p_quantity)
 end
 OrderItem Model
  belongs_to :product
  def unit_price
  if persisted?
   self[:unit_price]
  else
   Product.product_price?
  end
 end

我已经定义了关联,但是 OrderItem 的 Create 方法无法获取 product_id。产品有很多订单项目关系,请回答,我在做什么错误..? 谢谢

create_table "products", force: :cascade do |t|
    t.string "product_title"
    t.text "key_features"
    t.decimal "product_price"
    t.string "colour"
    t.string "main_material"
    t.integer "product_quantity"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

这只是一个简单的拼写错误。 在 OrderItem 模型的方法 unit_price 中,您使用的是 Product.prouct_price .

而不是你应该使用 product.product_price。第一个错误是拼写错误,第二个错误是因为您使用 Product 而不是 product 来将关联产品用于 OrderItem。

此外,我无法理解为什么您将 unit_pricetotal_price 保留在 OrderItem 的 table 中,因为您可以使用关联和简单的方式动态评估或计算出该值数学。因此,根据我的说法,没有必要为这些值制作列。