我正在使用 Rails 4.1.1、ruby 2.1、mongodb、mongoid 作为包装器,rails_admin 用于创建管理界面
我知道“attr_accessible”不再适用于 Rails4。所以我安装了“protected_attributes”gem。但仍然没有成功,我仍然在我的控制台中收到警告
[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
请任何人帮忙? 谢谢
最佳答案
Mongoid 4(撰写本文时 <= 4.0.2)不知道 protected_attributes
gem 提供的 ActiveModel::MassAssignmentSecurity
模块。
因此,您必须手动将行为包含在模型中,例如
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
关于ruby-on-rails-4 - 未定义的方法 `attr_accessible' 与 Mongoid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24444959/