我不斷收到以下錯誤,無法糾正它可以請別人幫忙。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
你不應該在Rails 4中使用'protected_attributes'(即attr_accessible),你應該使用'strong_parameters'。 – sevenseacat
有沒有幫助? –