2

我正在使用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.

请问我该如何解决?

4

2 回答 2

7

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
于 2012-12-29T22:03:02.670 回答
0

就我而言:

users_controller.rb

  before_action :logged_in?, only: :new

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

application_controller.rb

def logged_in?
    redirect_to users_new_url unless current_user.present?
end

当我尝试重定向到“用户/新”页面时,发生了同样的错误。这只是因为我试图重定向到“用户/新”页面和“def logged_in?” 也重定向到同一页面。

然后我像这样更改了application_controller.rb代码:

def logged_in?
    redirect_to root_url unless current_user.blank?
end

错误_已解决。

于 2020-09-29T11:53:51.737 回答