【问题标题】:Activating the has_secure_password [closed]激活 has_secure_password [关闭]
【发布时间】:2014-06-15 10:57:26
【问题描述】:

我正在尝试在我的用户模型中激活 rails has_secure_password。

我已经安装了 brypt gem:gem 'bcrypt', '~> 3.1.7'

我的模型如下

#Schema: User(name:string, password_digest:string)

class User < ActiveRecord::Base
  validates :name, presence: true, uniqueness: true
  has_secure_password
  validates_confirmation_of :password
end

当我在 rails 沙箱中运行以下命令时,我得到了(使用 rails 4.1.0):

user = User.new(name:'tbtb', password: 'tbtb', pasword_confirmation: 'tbtb')
ActiveRecord::UnknownAttributeError: unknown attribute: pasword_confirmation

user = User.new(name:'tbtb', password_digest: 'tbtb')
<User id: nil, name: "tbtb", password_digest: "tbtb", created_at: nil, updated_at: nil>

如果您不使用 has_secure_password,这与预期的一样。似乎 has_secure_password 没有被激活,password_digest 字段没有被 ActiveRecord 解释 (http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html)

似乎无法弄清楚我在这里做错了什么......

【问题讨论】:

  • pasword_confirmation 有一个 S?
  • 是的,请查看已接受的答案

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


【解决方案1】:

这是一个简单的错字,将pasword_confirmation更改为password_confirmation

user = User.new(name:'tbtb', password: 'tbtb', password_confirmation: 'tbtb')

【讨论】:

  • 哈,这是真的。谢谢
猜你喜欢
  • 2014-07-02
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多