如果可能的话,给出cart.rb的所有代码。也许您的add_product方法是在类似这样的私有方法下进行的。我知道这应该是评论,我想用例子来解释它,所以我把它粘贴在答案中。
private
def self.some_method
#some code
end
def add_product(product_id)
注释中的代码如下所示
class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy
end #this end is creating problem
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
关闭类后添加方法。将类的结尾放在方法的结尾之后,我敢打赌它会起作用。
将cart.rb更改为
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 #this end should after the end of class method