我有一个代码,一旦我创建订单,它将自动填写订单项的信息(因为每个订单有很多项)但我有错误未初始化的常量Order :: OrderItem。我该如何解决这个问题?
模型
class Order < ActiveRecord::Base
# attr_accessible :title, :body
attr_accessible :amount, :currency
has_many :order_items
end
class OrderItems < ActiveRecord::Base
attr_accessible :items, :order_id, :quantity
belongs_to :order
end
控制器
def checkout
@order = Order.new # Create new order
@order.total = @shopping_cart.total
@order.sub_total = @shopping_cart.subtotal
@order.sales_tax = @shopping_cart.taxes
@shopping_cart.shopping_cart_items.each do |cart_item|
@orderitems = @order.order_items.build(items: cart_item.item.name, quantity: cart_item.quantity)
end
end
答案 0 :(得分:1)
未初始化的常量Order :: OrderItem
问题在于这一行
class OrderItems < ActiveRecord::Base
Model class names
应该是 singular
。将OrderItems
更改为OrderItem
。也不要忘记改变 model file name
。