我已经看了一段时间,没有看到一个错误。我错过了什么?
当我将一个项目添加到购物车时,出现以下错误
我已经运行了迁移。
..
/Users/computername/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active\u record/associations/association\u collection.rb:376:in
method_missing'
/Users/computername/Documents/rails_projects/depot/app/models/cart.rb:5:in
添加产品'
/Users/computername/Documents/rails\u projects/depot/app/controllers/line\u items\u controller.rb:46:in
`创建'
这是我的创建方法
def create
@cart = find_or_create_cart
product = Product.find(params[:product_id])
#@line_item = @cart.line_items.build(:product => product)
@line_item = @cart.add_product(product.id)
..
我的购物车模型
class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
def add_product(product_id)
current_item = line_items.where(:product_id => product_id).first
if current_item
current_item.quantity += 1
else
current_item = LineItem.new(:product_id=>product_id)
line_items << current_item
end
current_item
end
end