设计:如何从db获取用户密码

And*_*aev 3 devise ruby-on-rails-4

我正在为用户登录编写黄瓜测试.

我有这样的场景背景,将用户添加到数据库

User.create!(

    :email => email,

    :password => password,

    :password_confirmation => password

    )
Run Code Online (Sandbox Code Playgroud)

那么,为什么在我的顺序步骤中,我得到@ user.password nil?

  @user = User.find_by :email => "some@email.com"

  ...

  fill_in('Email', :with => @user.email)

  fill_in('Password', :with => @user.password)
  ...
Run Code Online (Sandbox Code Playgroud)

Kir*_*rat 6

设计最初通过加密来存储原始密码.的encrypted_password(模型中的字段名)被存储在数据库中.

现在,当您调用User.find_by :email => "some@email.com"password字段时,该字段不存在.password不是模型用户中的字段.它是模型中存在的实例变量,仅在您提交login/sign up表单时设置.在您的情况下,您之前尝试访问密码字段,log in因此未设置实例变量,您将获得password为零.

如果您想了解更多详细信息,可以查看Devise密码的实际实现