我正在尝试创建 line_items 但我收到此错误
app/controllers/line_items_controller.rb:52:in `create'
引用此行
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)