我有user
模型使用devise gem,我没有attr_accessible
任何字段仍然会收到错误:
无法批量分配受保护的属性
我的User
课程如下
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:token_authenticatable #, :validatable
end
答案 0 :(得分:4)
我也有同样的问题,也许设计与attr_accessible
有关。您需要在模型中设置attr_accessible
才能使其正常工作。
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :token_authenticatable #, :validatable`
# Setup accessible (or protected) attributes for your model
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation
end
您可以查看这些railscast剧集。
答案 1 :(得分:2)
如果我放attr_protected
而不是attr_accessible
,那么它对我有效
仅供参考,我使用Rails 3.2.3和Ruby 1.9.3
答案 2 :(得分:0)
自Rails 3.2.3起,config.active_record.whitelist_attributes
中的config/application.rb
默认为true
。您必须为需要批量分配的属性手动设置attr_accessible
(或者您可以将whitelist_attributes
设置为false
以禁用此行为。)
答案 3 :(得分:0)
我只设置了attr_accessible:name,:password,:password_confirmation,它正在工作, 在config \ application.rb中没有设置config.active_record.whitelist_attributes = false 只检查attr_accessible中