has_secure_password prevents setting a boolean to true

你离开我真会死。 提交于 2019-12-25 05:00:43

问题


I want to do email confirmation, after having set up sign in/sign up and sessions using the has_secure_password and authenticate method. I have a user model, and I added a confirmed boolean. When a user is created, I set their confirmed boolean to false, and send them an email with a link. Clicking the link generates a GET request to /users/:id/confirm, and executes the code of "confirm" action in the users_controller that is the following :


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

Very simple (I will do verification token later). My problem is that my user is never saved.

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

How can I change a user object without having to edit their password each time ? Any help would be greatly appreciated.

Thanks !


回答1:


Try using update_attribute instead of update_attributes.

  @user.update_attribute(:confirmed, true)


来源:https://stackoverflow.com/questions/11386092/has-secure-password-prevents-setting-a-boolean-to-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!