How do I keep the user signed in after they update

2019-05-03 12:25发布

问题:

This question already has an answer here:

  • Devise logging out automatically after password change 8 answers

Whenever a user updates their password, it signs the user out and asks them to sign in again. This is my action

users_controller.rb
  def update
    @user = User.find(params[:id])
    if @user.update user_params
      sign_in @user, bypass: true # for some reason Devise signs the user out
      redirect_to @user

http://www.rubydoc.info/github/plataformatec/devise/Devise/Controllers/SignInOut#sign_in-instance_method

I tried sign_in @user, also, but that didn't work. I tried it without sign_in but that didn't work either. I saw this answer, but it doesn't help: Devise is logging out users after a password change. Updating the user without the password is working OK. (There is some code not shown.)

Devise 3.4.1.

回答1:

I had to add a 'scope`. This worked.

sign_in :user, @user, bypass: true # for some reason Devise signs the user out

See answer here: https://stackoverflow.com/a/11589286/148844