2012-11-22 105 views
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) 

回答

3

您必須添加attr_accessible :product在您的LineItem類中。

這是迫使你白名單哪些字段可以大量分配,以避免像github上黑客有一個安全;)

+0

didnt工作!現在就試試! =/ – thiagoh

+0

Mhh如果你使用:product代替? – Intrepidd

+0

我應該在哪裏使用它? – thiagoh

相關問題