Ruby 在 rails 上,@current_user 在重新加载网站或更改页面时设置为空

Ruby on rails, @current_user is set null when reloding webseite or changing page

我在 rails 上创建了一个 Ruby 应用程序,您可以在其中创建一个帐户。当您随后登录时,将创建一个会话。在 ApplicationController.rb 中有一个 before_action 用于检查用户是否已登录,除了视图注册和欢迎。我有一个 @current_user 变量,当有用户登录时该变量为真。 登录工作正常。当您按下登录按钮时,会创建一个会话,但只要我重新加载页面或单击 link 到另一个页面,@current_user 就等于 null。 会话仍然存在,但@current_user 为假。

如何让会话持续到用户单击注销按钮?或者如何修复我的@current_user 变量?

感谢您的帮助!

application_controller.rb

class ApplicationController < ActionController::Base
  skip_before_action :verify_authenticity_token
  before_action :require_login, except: [:welcome, :register, :create]

  private

  def require_login
    unless @current_user
      #reset_session
      flash[:error] = "You must be logged in"
      render json: {status: "You are not logged in", user: @current_user, session: session}
      #render sessionhandling_welcome_path
    end
  end
end

current_user_concern.rb

module CurrentUserConcern
  extend ActiveSupport::Concern

  included do
    before_action :set_current_user
  end

  def set_current_user
    if session[:user_id]
      @current_user = User.find(session[:user_id])
    end
  end
end

sessions_controller.rb

class SessionsController < ApplicationController
  include CurrentUserConcern

  def create
    user = User.find_by(email: params["user"]["email"]).try(:authenticate, params["user"]["password"])
    if user
      session[:user_id] = user.id
      session[:expires_at] = Time.current + 1.minutes
      #render "sessionhandling/home"
      render json: {status: :created, logged_in: true, user: user, expires_at: session}
    else
      render json: {status: 401}
    end
  end

  def logged_in
    if @current_user
      render json: {logged_in: true, user: @current_user}
    else
      render json: {logged_in: false}
    end
  end

  def logout
    reset_session
    render json: {status: 200, logged_out: true}
  end
end

home.html.erb

<h1>Home</h1>
<%= link_to "Controllpanel", sessionhandling_controllpanel_path %>
<%= link_to "Logout", logout_path %>

您缺少这一部分:

application_controller.rb

class ApplicationController < ActionController::Base
  include CurrentUserConcern # This is missing

  skip_before_action :verify_authenticity_token
  before_action :require_login, except: [:welcome, :register, :create]

  private

  def require_login
    unless @current_user
      #reset_session
      flash[:error] = "You must be logged in"
      render json: {status: "You are not logged in", user: @current_user, session: session}
      #render sessionhandling_welcome_path
    end
  end
end

另外,我也会更改 CurrentUserConcern 来处理 #require_login。无法保证 before_action 将首先 运行,require_loginset_current_user,即使有保证也会令人困惑。