簡體   English   中英

錯誤:重定向過多

[英]error: Too many redirects

我正在使用devise並嘗試以下操作:

class ApplicationController < ActionController::Base
   protect_from_forgery
   before_filter :is_worker

   def is_worker
     if user_signed_in?
        @email = current_user.email
        if @email && Worker.find_by_email(@email).nil?
          redirect_to '/tasksadmins'
        else
           redirect_to '/workers'
        end
     else
         redirect_to '/users/sign_in'
     end
   end
end

當我嘗試進入站點時: localhost:3000/tasksadmins ,我得到:

Oops! It was not possible to show this website

The website at http://localhost:3000/tasksadmins seems to be unavailable. The precise error was:

Too many redirects

It could be temporarily switched off or moved to a new address. Don't forget to check that your internet connection is working correctly.

請問我該如何解決?

before_filter應用於每個請求。 這就是為什么它一次又一次地重定向。

您可能只想過濾特定操作:

before_filter :is_worker, only: :index

另一種解決方案是檢查#is_worker是否需要重定向:

redirect_to '/workers' unless request.fullpath == '/workers'

編輯:

另一種方法是跳過重定向的目標操作的 before 過濾器。 例子:

class WorkersController < ApplicationController

  skip_before_filter :is_worker, only: :index

  # …

end

就我而言:

用戶控制器.rb

  before_action :logged_in?, only: :new

  def new
    @user = User.new
    render layout: "session"
  end

應用控制器.rb

def logged_in?
    redirect_to users_new_url unless current_user.present?
end

當我嘗試重定向到“用戶/新”頁面時,發生了同樣的錯誤。 這只是因為我試圖重定向到“用戶/新”頁面和“定義已登錄?” 也重定向到同一頁面。

然后我像這樣更改了application_controller.rb代碼:

def logged_in?
    redirect_to root_url unless current_user.blank?
end

錯誤_已解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM