【问题标题】:has_secure_password prevents setting a boolean to truehas_secure_password 防止将布尔值设置为 true
【发布时间】:2012-07-08 07:44:37
【问题描述】:

在使用 has_secure_password 和 authentication 方法设置登录/注册和会话后,我想进行电子邮件确认。 我有一个用户模型,我添加了一个确认的布尔值。创建用户后,我将其确认的布尔值设置为 false,并向他们发送一封带有链接的电子邮件。单击该链接会生成对 /users/:id/confirm 的 GET 请求,并在 users_controller 中执行“确认”操作的代码,如下所示:


def confirm
    @user = User.find(params[:id])
    @user.update_attributes(confirmed: true)
    if @user.save 
      sign_in(@user)
      flash[:success] = "Your account has been successfully confirmed"
    else
      flash[:error] = "There has been a problem confirming your account "
    end 
     redirect_to root_path
  end

很简单(我稍后会做验证令牌)。我的问题是我的用户永远不会被保存。

@user.errors.full_messages
返回:
["Password is too short", "Password confirmation can't be blank"]

如何更改用户对象而无需每次都编辑其密码? 任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: ruby-on-rails email-verification


    【解决方案1】:

    尝试使用 update_attribute 而不是 update_attributes。

      @user.update_attribute(:confirmed, true)
    

    【讨论】:

    • ...不要保存两次。 update_attribute 已经进行了保存——无需致电 @user.save。去掉后者,改为检查update_attribute的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2014-01-19
    • 2013-06-22
    • 1970-01-01
    • 2015-11-27
    • 2019-09-29
    相关资源
    最近更新 更多