问题
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