3

line_items を作成しようとしていますが、このエラーが発生しています

app/controllers/line_items_controller.rb:52:`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 に答える