2013-12-12 56 views
1

我不斷收到以下錯誤,無法糾正它可以請別人幫忙。Rails 4中的質量分配問題

::加載ActiveModel :: MassAssignmentSecurity中的錯誤HomePageController#add_to_cart 無法大規模分配的CartItem保護屬性:CUSTOMER_ID,PRODUCT_ID,quantity_ordered

提取的源(左右線#7):

高清add_product(ID,PRODUCT_ID,quantity_ordered) cart_item = CartItem.new( :CUSTOMER_ID => ID, :PRODUCT_ID => PRODUCT_ID, :quantity_ordered => quantity_ordered)

class Customer < ActiveRecord::Base 
    has_many :cart_items, :dependent=> :destroy 
    attr_accessible :customer_id, :product_id, :quantity_ordered 


    def add_product(id, product_id, quantity_ordered) 
    cart_item =CartItem.new(
     :customer_id => id, 
     :product_id => product_id, 
     :quantity_ordered => quantity_ordered) 
    cart_items << cart_item  #appends a value 
    cart_item    #returns a value 
end 

    def total_price 
    cart_items.to_a.sum { |item| item.sub_total } 
    end 
end 
+1

你不應該在Rails 4中使用'protected_attributes'(即attr_accessible),你應該使用'strong_parameters'。 – sevenseacat

+0

有沒有幫助? –

回答

0

它看起來像你的attr_accessible線應該是你CartItem模式,而不是客戶模型

0

在你CartItem.rb文件的頂部,你應該有這樣的事情:

class CartItem < ActiveRecord::Base 
    belongs_to :customer 
    attr_accessible :customer_id, :product_id, :quantity_ordered 

的attr_accessible在您的CartItem模型文件中是必需的,以便您可以在創建CartItem的新實例時分配這些屬性。

1

如果您想使用Rails-3樣式的受保護屬性,您需要安裝protected_attributes gem

正如Rafael Fiuza指出的那樣,如果您不想安裝另一個gem來獲得保護屬性的Rails-3方法,您將不得不在Rails 4中使用strong_parameters

如果你用強的參數,你在HomePageController代碼看起來是這樣的:

def add_to_cart 
    #code for defining @customer 
    @customer.cart_items.create(cart_item_params) 
end 

def cart_item_params 
    params.require(:cart_item).permit(:product_id, :quantity_ordered) 
end 

你並不需要在你的客戶模型中定義#add_product或者你可以重構它利用cart_items.create(cart_item_attributes而不是使其更短,更清潔。

如果您已經使用protected_attributes寶石,你需要添加下面的代碼在你的CartItem模型,而不是在您的客戶模型:

class CartItem < ActiveRecord::Base 
    attr_accessible :customer_id, :product_id, :quantity_ordered 

    #other code 
end 

希望它能幫助!

0

奇怪的是,爲什麼你會得到ActiveModel::MassAssignmentSecurity::Error例外。雖然,在質量分配是你的網站的一個可能的漏洞,它需要固定的幾個步驟:

  1. 在模型替換所有ID的聲明從attr_accessibleattr_protected操作:

    attr_protected:CUSTOMER_ID,:PRODUCT_ID

  2. 不通過ID字段到模型的構造,可以是new, '創造',等等:

    cart_item = CartItem.new quantity_ordered:quantity_ordered

  3. 可能的話,不直接分配ID字段,使用idless分配,而不是:

    cart_item.customer =客戶 cart_item.product =產品

  4. 此外,你的shell指定customer_id,product_id字段爲CartItem型號,不適用於Customer

至於結果,我們得到了CartItem模型下面的代碼:

class CartItem < ActiveRecord::Base 
    attr_protected :customer_id, :product_id, :quantity_ordered 
end 

而對於Customer型號:

class Customer < ActiveRecord::Base 
    has_many :cart_items, :dependent => :destroy 

    def add_product customer, product, quantity_ordered 
    cart_item = CartItem.new quantity_ordered: quantity_ordered 
    cart_item.customer = customer 
    cart_item.product = product 
    cart_item.save! 
    cart_items << cart_item  #appends the value 
    cart_item     #returns the value 
    end 

    def total_price 
    cart_items.to_a.sum { |item| item.sub_total } 
    end 
end 

此外,請更準確地檢查出了大衆賦值漏洞herehere

+0

@ doug-kandl我的回答有幫助嗎? –