Ruby-on-Rails:如何摆脱"你被重定向"页面

Dav*_*vid 18 redirect ruby-on-rails http-status-codes devise http-status-code-401

我正在重写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
Run Code Online (Sandbox Code Playgroud)

编辑

或者,我想显示flash消息并保留在同一页面上,但添加以下代码行:

render :text => "unauthorized", :status => 401
Run Code Online (Sandbox Code Playgroud)

让红宝石抱怨:

undefined method `render' for #<CustomFailure:0x00000103367f28>
Run Code Online (Sandbox Code Playgroud)

这里发生了什么事?

小智 32

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

  • Thx我有同样的尝试`redirect_to root_url,:status =>:unauthorized`.我不得不删除:状态选项. (2认同)