Abram
Abram

Reputation: 41944

Redirect with status: 401 hangs on "You are being redirected." How do I force redirect?

I am seeing the following page on unauthorized 401 redirection using rails 4:

enter image description here

I am fine with this page rendering, but not OK with the page hanging.

The link with anchor text 'redirected' works fine in redirecting to the correct page, but it must be clicked. In other words, the message is incorrect, and I am not being actively redirected.

How do I force redirect?

Here is the code that enables this, in 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

Upvotes: 1

Views: 390

Answers (1)

Leon
Leon

Reputation: 1292

Add this to your view

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

Or maybe something like this in your controller

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

Upvotes: 1

Related Questions