gpt4 book ai didi

ruby-on-rails - 错误 : Too many redirects

转载 作者:行者123 更新时间:2023-12-01 16:41:24 27 4
gpt4 key购买 nike

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

编辑:

另一种方法是跳过重定向目标操作的前置过滤器。示例:

class WorkersController < ApplicationController

skip_before_filter :is_worker, only: :index

# …

end

关于ruby-on-rails - 错误 : Too many redirects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14086244/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com