嗨,对不起,如果这是一个愚蠢的问题,真的我是ROR世界的新手,我正在努力学习。没有对象的NoMethodError
我阅读使用Rails电子书的敏捷Web农业开发并按照本书的程序我得到的股票与此错误:
NoMethodError in Line itemsController#create
You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.
这是车型号
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
这是行项目控制器正在调用哪种方法
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to(@line_item.cart, :notice => 'Line item was successfully created.') }
format.xml { render :xml => @line_item.cart, :status => :created, :location => @line_item }
else
format.html { render :action => "new" }
format.xml { render :xml => @line_item.errors, :status => :unprocessable_entity }
end
end
end
Rails版本3.0.5 红宝石版本1.8.7
任何建议?你能看到什么是错的吗? 谢谢
请发帖行号。 – 2011-04-15 18:55:07
发布堆栈跟踪的完整错误,您忽略了关键信息。 – 2011-04-15 19:41:22
在发布错误的行号之前,您不会获得任何有用的帮助。 – sawa 2011-04-15 19:50:35