当认证失败时,我想重定向到另一个 URL。
有什么想法吗?谢谢!
plugins
myplugin
lib
auth
my_auth_controller.rb
def after_authenticate(auth_hash)
if not auth
redirect_to "https://example.com"
end
result = Auth::Result.new
end
- 在您的代码中,您有一个 if not auth 条件,但没有相应的 else 语句。
- redirect_to 方法应该在控制器操作中使用。如果您在 after_authenticate 等单独的方法中使用它,请确保它在控制器的范围内。
因此,这是正确的代码。
def after_authenticate(auth_hash)
if not auth
redirect_to "https://example.com"
else
# 身份验证成功,继续进行身份验证过程
result = Auth::Result.new
end
end
结果是,如果身份验证失败 auth 不为 true,它将重定向到“https://example.com”。否则,如果身份验证成功,它将继续进行身份验证过程。
为了验证,您可以检查浏览器开发者工具以检查网络请求和重定向,或使用任何在线工具,例如redirect checker。这可以帮助您识别重定向问题和状态码。