未定义的方法`attr_accessible'与Mongoid

时间:2014-06-27 06:03:48

标签: ruby-on-rails-4 mongoid rails-admin attr-accessible

我使用Rails 4.1.1,ruby 2.1,mongodb,mongoid作为包装器,rails_admin用于创建管理界面

我知道' attr_accessible'不再适用于Rails4。所以我已经安装了' protected_attributes'宝石。但仍然没有成功,我仍在控制台收到警告

[RailsAdmin] Could not load model Company, assuming model is non existing. (undefined method `attr_accessible' for Company:Class)

因此,rails admin不加载类Company,因为我在模型中定义了attr_accessible。这是我公司的模特。

  class Company
  include Mongoid::Document

  @@employees_strength = {0 => '0-10', 1 => '11-50', 2 => '51-100', 3 => '101-500', 4 => '501-1000', 5 => '1000+', 6 => '5000+'}

  field :name,          type: String
  field :website,       type: String
  field :domain_name,   type: String
  field :strength,      type: Integer

  has_many :employees
  has_one :admin, :class_name => 'Employee',  :dependent => :destroy, :inverse_of => :organization

  #attr_accessible :name, :website, :domain_name, :strength#, :admin_attributes, :allow_destroy => true
  attr_accessible :admin_attributes
  accepts_nested_attributes_for :admin, :allow_destroy => true
 end

请任何身体都可以帮忙吗? 感谢

1 个答案:

答案 0 :(得分:2)

Mongoid 4(在撰写本文时<= 4.0.2)不知道ActiveModel::MassAssignmentSecurity gem提供的protected_attributes模块。

因此,您必须手动在模型中包含行为,例如

class SomeDocument
  include Mongoid::Document
  include ActiveModel::MassAssignmentSecurity

  field :some_field
  attr_accessible :some_field
end

然而,这很快就会变得乏味,所以合理的替代方案是在定义任何模型之前将模块包含在Mongoid::Document模块中。

module Mongoid
  module Document
    include ActiveModel::MassAssignmentSecurity
  end
end
相关问题