2016-08-24 47 views
0

我看到下面的页面上未经授权使用401个重定向轨道4,5:重定向状态:401挂在“您正在重定向。”我如何强制重定向?

enter image description here

我很好此页面的渲染,但不与网页挂确定。

带锚文本“重定向”的链接可以在重定向到正确的页面时正常工作,但必须单击该链接。换句话说,该消息是不正确的,我没有被主动重定向。

如何强制重定向?

这里是可以实现这一目的的代码,在action_controller/metal

def redirect_to(options = {}, response_status = {}) #:doc: 
    raise ActionControllerError.new("Cannot redirect to nil!") unless options 
    raise AbstractController::DoubleRenderError if response_body 

    self.status  = _extract_redirect_to_status(options, response_status) 
    self.location  = _compute_redirect_to_location(request, options) 
    self.response_body = "<html><body>You are being <a href=\"#{ERB::Util.unwrapped_html_escape(location)}\">redirected</a>.</body></html>" 
end 
+1

使用JavaScript与setTimeout – Leon

+0

好主意,但我怎么让js到ru n在布局中:仅用于此视图的false环境 – Abram

+0

将其添加到视图本身并使用vanilla JS作为jquery可能未加载。 – Leon

回答

1

添加到您的视图

<script> 
setTimeout(function(){ window.location.href = 'your_url_here' }, 3000); 
</script> 

或者,也许这样的事情在你的控制器

def redirect_to(options = {}, response_status = {}) 
    super 
    self.response_body = "<html><body><script>setTimeout(function(){ window.location.href = \"#{ERB::Util.unwrapped_html_escape(location)}\" }, 3000);</script>You are being <a href=\"#{ERB::Util.unwrapped_html_escape(location)}\">redire‌​cted</a>.</body></ht‌​ml>" 
end 
+0

实际上没有任何视图文件可以添加它,并且它不会从没有布局的资产运行。 – Abram

+0

self.response_body = “您正在redirected”。将这项工作? – Leon

+0

如何在不改变导轨基本代码的情况下重写此内容 – Abram