3
我想創建line_items但我得到這個錯誤Ruby on Rails的::加載ActiveModel :: MassAssignmentSecurity錯誤
應用程序/控制器/ line_items_controller.rb:52:在'創造」
其中引用這條線
Can't mass-assign protected attributes: product
@line_item = @cart.line_items.build(:product => product)
完整代碼如下
class Product < ActiveRecord::Base
attr_accessible :description, :image_url, :price, :title
default_scope :order => 'title'
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
#more code here...
private
# ensure that there are no line items referencing this product
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
end
def create
@cart = current_cart
product = Product.find(params[:product_id])
#the error is HERE!!
@line_item = @cart.line_items.build(:product => product)
didnt工作!現在就試試! =/ – thiagoh
Mhh如果你使用:product代替? – Intrepidd
我應該在哪裏使用它? – thiagoh