Ruby Can';t mass assign protected attributes-it';属性存取器中的质量分配字段

Ruby Can';t mass assign protected attributes-it';属性存取器中的质量分配字段,ruby,ruby-on-rails-3,model,mass-assignment,attr-accessor,Ruby,Ruby On Rails 3,Model,Mass Assignment,Attr Accessor,无法批量分配受保护的属性:密码、密码\u确认 这两个字段都没有映射到数据库中,它们只是表单中的字段,我想使用它们来启用一些良好的验证 这是我的模型课: class User < ActiveRecord::Base attr_accessible :email, :password_hash, :password_salt attr_accessor :password, :password_confirmation before_save :encrypt_passwor

无法批量分配受保护的属性:密码、密码\u确认


这两个字段都没有映射到数据库中,它们只是表单中的字段,我想使用它们来启用一些良好的验证

这是我的模型课:

class User < ActiveRecord::Base
  attr_accessible :email, :password_hash, :password_salt
  attr_accessor :password, :password_confirmation

  before_save :encrypt_password

  validates_confirmation_of :password
  validates :password, presence: true
  validates :email, presence: true

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end

我这里缺少什么?

您需要在属性可访问性中添加:password\u confirmation:password

属性可访问性指定可通过批量分配设置的模型属性白名单。 attr_accessor创建实例变量(@name)和相应的访问方法来读取它。还创建一个名为name=的方法来设置属性

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation
  attr_accessor :password

  before_save :encrypt_password

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates :email, presence: true

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end
class用户:create
验证:电子邮件,状态:true
def加密密码
如果密码存在?
self.password\u salt=BCrypt::Engine.generate\u salt
self.password\u hash=BCrypt::Engine.hash\u secret(密码、密码)
结束
结束
结束
class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation
  attr_accessor :password

  before_save :encrypt_password

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create
  validates :email, presence: true

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
end