我正在通過Agile Web Dev w/Rails圖書(第4版)工作,並且我完全卡住了... 我正在運行Rails 3.2.3 on Mac OSX。 IM在任務d-3:添加一個按鈕 ....它開始與測試:在本章結尾沒有工作函......它給了我一個錯誤說:帶有Rails的敏捷Web開發(第4版)購物車問題
Can't Mass assign protected attributes: product
我其次這裏給出的建議是:http://forums.pragprog.com/forums/148/topics/10565
,並改變了我的代碼行中Line_Items_Controller到
@line_item = @cart.line_items.build
@line_item.product = product
這裏是我當前Line_Items_Controller創建方法如下所示:
# POST /line_items
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.line_items.build
@line_item.product = product
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,
:status => :created, :location => @line_item }
else
format.html { render :action => "new" }
format.xml { render :xml => @line_item.errors,
:status => :unprocessable_entity }
end
end
end
現在我得到這個奇怪的消息:
NoMethodError in LineItemsController#create undefined method `product=' for <LineItem:0x000001024f7fb0>
,這裏是我的LineItem模型
class LineItem < ActiveRecord::Base
attr_accessible :cart_id, :product_id, :product
end
林真的不知道該怎麼在這一點上做的,因爲我是一個總Rails(& Ruby)newb。 任何人都可以指向正確的方向嗎?
需要查看LineItem模型。 – meagar