3

我正在尝试创建 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)
4

1 回答 1

3

您必须添加attr_accessible :productLineItem 类。

这是一种安全措施,它迫使您将可以批量分配的字段列入白名单,以避免像 github 这样的黑客攻击;)

于 2012-11-22T20:04:06.557 回答