Rails 6: localhost 重定向你的次数太多

Rails 6: localhost redirected you too many times

我的应用程序的根页面是 post 的索引页面

Rails.application.routes.draw do
  root 'posts#index'
  resources :users
  resources :posts 
  resource :sessions, only: [:new, :create, :destroy]
  resources :passwords, only: [:new, :create, :edit, :update]
end

My 使用 cookie 来存储用户会话。在登录时,如果用户勾选记住我复选框,它的会话将被记住,他可以在关闭浏览器后访问该应用程序,而无需再次登录。

我遵循的步骤: 1. 运行 rails 在终端上。

  1. 在浏览器地址栏输入localhost:3000

  2. 应用程序打开,我登陆 posts#index 页面,因为它是我的应用程序遵循的根路径。

  3. 我点击登录 link,我填写电子邮件、密码 & 勾选记住我复选框

  4. 我登陆了用户的个人资料页面,即 users#show page

终端到此为止

  1. 现在我关闭浏览器window。 (我没有注销。)(cookie 应该记住我的详细信息)

  2. 我再次打开浏览器。在浏览器的地址栏中输入 localhost:3000。

我登陆 'posts#index' 页面。但我不想在用户的个人资料上,即 users#show 页面,因为我的详细信息存储在 cookies

但我想登陆用户的个人资料页面。

在应用程序控制器上,如果...

 before_action :set_cache_buster,:redirect_if_logged_in

  def redirect_if_logged_in
    redirect_to user_path(current_user) if logged_in?
   end

如果说 重定向太多次

,请登录

应用程序控制器:

# frozen_string_literal: true

class ApplicationController < ActionController::Base


  protect_from_forgery with: :exception

  helper_method :current_user,:logged_in?, :logged_in_user,:current_user

  before_action :set_cache_buster
   def log_in(user)
     session[:user_id] = user.id
   end

  def current_user
     @current_user ||= User.where("auth_token =?", cookies[:auth_token]).first if cookies[:auth_token]
  end

   def logged_in?
     !current_user.nil?
   end

   def logged_in_user
     unless logged_in?
       flash[:danger] = "Please log in."
       redirect_to new_sessions_path
     end
   end

   def current_user?(user)
     user == current_user
    end

    private
    def set_cache_buster
      response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
      response.headers["Pragma"] = "no-cache"
      response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
    end


end

用户控制器

# frozen_string_literal: true
require 'resolv-replace'


class UsersController < ApplicationController
  before_action :logged_in_user, only: [:show, :edit, :update]
  before_action :correct_user, only: [:show, :edit, :update]



  def index
    @users = User.all
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(set_params)
    if @user.save
      UserNotifierMailer.send_signup_email(@user).deliver
      flash[:success] ="Success"
      redirect_to new_sessions_path
    else
      render 'new'
    end
  end

  def show
    @user = User.find(params[:id])
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update(update_params)
      redirect_to @user
    else
      render 'edit'
   end
  end


  private

  def set_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end

  def update_params
    params.require(:user).permit(:name, :email, :password, :password_confirmation)
  end

  def correct_user
    @user = User.find(params[:id])
    rescue ActiveRecord::RecordNotFound
    redirect_to(@current_user) unless current_user?(@user)
  end

end

在我输入 localhost:3000 后重定向到 'post#index' 就可以了。但我添加了一个条件重定向。如果 cookie 不为空,则重定向到其他路径。

我希望我至少现在能够很好地解释这个问题。

尝试这样做,添加 before_action

before_action :check_logged_in

def check_logged_in
  unless logged_in?
    flash[:danger] = "Please log in."
    redirect_to new_sessions_path
  else
    redirect_to user_path(current_user) if cookies[:auth_token]
  end
end

添加这个

后你不需要logged_in_user作为辅助方法

希望对您有所帮助!

您每次被重定向时都会调用 redirect_if_logged_in 方法,因为您的 UsersController 继承自 ApplicationController。据我了解,您只需将此代码移至 PostsController