2016-08-24 44 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