我正在覆盖 Devise 的失败响应,以便我可以设置 401 状态代码。但是,当用户登录失败时,他们将被重定向到带有“您正在被重定向”链接的页面。如果我删除此 :status => 401
从重定向它工作正常。
class CustomFailure < Devise::FailureApp
def redirect_url
new_user_session_url(:subdomain => 'secure')
end
def respond
if http_auth?
http_auth
else
store_location!
flash[:alert] = i18n_message unless flash[:notice]
redirect_to redirect_url, :status => 401
end
end
end
编辑
或者,我想显示 flash 消息并保持在同一页面上,但添加以下代码行:
render :text => "unauthorized", :status => 401
导致 ruby 提示:
undefined method `render' for #<CustomFailure:0x00000103367f28>
这里发生了什么事?
最佳答案
重定向的正确 HTTP 状态采用 30x 形式(301 和 302 是最常用的)。默认情况下,redirect_to 帮助程序在 HTTP 响应上设置 302 状态 header 。如果您覆盖它并将其设置为 401,您的 Web 浏览器将假定响应是常规网页并呈现响应正文——在重定向中,它是样板文本“您正在被重定向”。
关于Ruby-on-Rails:如何摆脱 "you are being redirected"页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4310913/