Rails中的批量分配问题4

时间:2013-12-12 02:57:13

标签: ruby-on-rails ruby ruby-on-rails-4

我一直收到以下错误,无法纠正,有人可以帮忙。

ActiveModel :: MassAssignmentSecurity :: HomePageController中的错误#add_to_cart 无法为CartItem大量分配受保护的属性:customer_id,product_id,quantity_ordered

提取的来源(第7行):

def 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

5 个答案:

答案 0 :(得分: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 gem,则需要在CartItem模型中添加以下代码,而不是在Customer模型中:

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

  #other code
end

希望它有所帮助!

答案 1 :(得分:0)

看起来您的attr_accessible行应该在您的CartItem模型中,而不是客户模型

答案 2 :(得分:0)

在CartItem.rb文件的顶部,你应该有这样的东西:

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

您的CartItem模型文件中需要attr_accessible,以便您在创建CartItem的新实例时分配这些属性。

答案 3 :(得分:0)

在轨道4中,质量分配保护在使用强参数的控制器中。查看http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html

答案 4 :(得分:0)

奇怪的是,为什么会得到ActiveModel::MassAssignmentSecurity::Error例外。虽然,质量分配是您网站的一个可能的漏洞,但需要通过以下几个步骤进行修复:

  1. 将模型中所有ID的声明替换为attr_accessibleattr_protected运算符:

      

    attr_protected:customer_id,:product_id

  2. 不要将id字段传递给模型的构造函数,可以是new,'create'等:

      

    cart_item = CartItem.new quantity_ordered:quantity_ordered

  3. 可能不要直接指定ids字段,而是使用无字分配:

      

    cart_item.customer =客户   cart_item.product = product

  4. 此外,您为customer_id模型指定了product_idCartItem字段,而不是Customer

  5. 结果我们得到了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