2017-02-07 35 views
1

当用户创建密码update时,操作被击中。设计sign_in(:user,@user)401错误

module Users 
    class PasswordController < ApplicationController 
    skip_before_action :authenticate_user! 
    before_action :set_user 

    def setup 
     render file: "#{Rails.root}/public/404" unless @user&.sign_in_count&.zero? 
    end 

    def update 
     if @user.update_attributes(password_params) 
     sign_in(:user, @user) 
     redirect_to root_path, notice: 'Setup complete' 
     else 
     flash[:alert] = errors(@user) 
     render :setup 
     end 
    end 

    private 

    def password_params 
     params.require(:user).permit(:password, :password_confirmation) 
    end 

    def set_user 
     @user = User.find_by(confirmation_token: params[:token]) 
    end 
    end 
end 

update行动被击中。

这是控制台显示

Started PATCH "https://stackoverflow.com/users/password/wynEjv-E7uFp44EcwjbS" for ::1 at 2017-02-07 12:33:01 +1000 
Processing by Users::PasswordController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"vIyPtidsM1GDNLh3n7gDDB2HzvR0QEEwAAvfMybNRn0/t4em4StiUVDyKwLC9i1OThoYguEae4T+xqLttjt1Pw==", "email"=>"[email protected]", "user"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Continue", "token"=>"wynEjv-E7uFp44EcwjbS"} 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."confirmation_token" = $1 LIMIT $2 [["confirmation_token", "wynEjv-E7uFp44EcwjbS"], ["LIMIT", 1]] 
    (0.1ms) BEGIN 
    SQL (0.5ms) UPDATE "users" SET "encrypted_password" = $1, "updated_at" = $2 WHERE "users"."id" = $3 [["encrypted_password", "$2a$11$o5wmmxnCv.rlPha52GR0IOG4tbhEJYNNF9tctcSLDMS/dvLAU0hXq"], ["updated_at", 2017-02-07 02:33:01 UTC], ["id", 9]] 
    (1.0ms) COMMIT 
    User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 9], ["LIMIT", 1]] 
    (0.1ms) BEGIN 
    (0.2ms) COMMIT 
Completed 401 Unauthorized in 129ms (ActiveRecord: 2.5ms) 

有谁知道为什么sign_in方法不在这里工作?

我用它在几个应用程序,然后它的工作。

感谢您提前给予任何帮助。

+1

不要担心它的所有修复。用户需要首先使用设计进行确认。 – joeyk16

回答

1

您可以使用它像这样

@user.update_with_password(password_params) 
sign_in @user, :bypass => true 

希望它会为你工作,因为它为我工作!

+0

嗨,是的,我试过它没有工作。它可能适用于你,因为你的用户已经确认。 – joeyk16

+0

您是否想要确认用户以及密码更新? –

+0

是的,我只是在更新操作中做了@ user.confirm。它的工作现在。 – joeyk16

相关问题