Ruby-on-Rails:如何摆脱“你被重定向"的问题页 [英] Ruby-on-Rails: How to get rid of "you are being redirected" page

查看:21
本文介绍了Ruby-on-Rails:如何摆脱“你被重定向"的问题页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在覆盖 Devise 的失败响应,以便我可以设置 401 状态代码.但是,当用户登录失败时,他们将被重定向到带有您正在被重定向"链接的页面.如果我从重定向中删除这个 :status => 401 它工作正常.

I am overriding Devise's failure response so that I can set a 401 status code. However, when the user fails to sign in, they are redirected to a page with a "you are being redirected" link. If I remove this :status => 401 from the redirect it works fine.

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 消息并保持在同一页面上,但添加以下代码行:

edit

Alternatively I would like to display the flash message and remain on the same page but adding this line of code:

render :text => "unauthorized", :status => 401

导致 ruby​​ 抱怨:

causes ruby to complain:

undefined method `render' for #<CustomFailure:0x00000103367f28>

这里发生了什么?

推荐答案

重定向的正确 HTTP 状态采用 30x 形式(301 和 302 是最常用的).默认情况下,redirect_to 帮助程序在 HTTP 响应上设置 302 状态标头.如果您覆盖它并将其设置为 401,您的 Web 浏览器将假定响应是一个常规网页,并将呈现响应正文——在重定向中,它是样板文本您正在被重定向".

Proper HTTP statuses for a redirection are in the 30x form (301 and 302 being the most frequently used). By default, the redirect_to helper sets a 302 status header on the HTTP response. If you override that and set that to a 401, your web browser will assume that the response is a regular web page and will render the response body --which, in a redirection, is the boilerplate text "You are being redirected".

这篇关于Ruby-on-Rails:如何摆脱“你被重定向"的问题页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆