设计sign_in(:user,@ user)401错误

时间:2017-02-07 02:37:28

标签: ruby-on-rails devise

当用户创建密码时,update操作被点击。

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 "/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"=>"test04@example.com", "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 个答案:

答案 0 :(得分:1)

您可以像这样使用

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

希望它对你有用,因为它对我有用!