Frage

So I'm trying to redirect to my root_url, but from several partials deep. The redirect works but it only redirects within the partial. It worked fine before implementing partials. To redirect I have a before_filter that checks the session and redirects to root_url if the session is no longer valid.

Another note: I am making ajax calls to return other partials (like loading divs created server side) and it seems like after the session expires instead of returning the partial's html it returns the login html its supposed to redirect to. This is so annoying.

So application.html.erb

<div id="main-contain" class="container">
    <%= yield %>
</div>

renders _content.html.erb

<%= render partial: 'partials/content' %>

which renders _my_partial.html.erb

<%= render partial: 'partials/my_partial' %>

and when the session ends the redirect only works within the deepest partial.

Any ideas? I've searched up and down after scratching my head for too long.

Edit:

Here is the before filter function

unless is_authorized

      flash[:content] = { title: 'Login Required', message: 'You must log in to access dash.'  } 
      redirect_to '/gate'  

end

where 'gate' is my login page. This is the route:

match 'gate' => 'gate#latch'
War es hilfreich?

Lösung

Figured it out :) after much head scratching. I could only seem to return the login page as html rather than redirecting. My solution:

in my authentication check:

unless is_authorized

  respond_to do |format|
    format.html { redirect_to '/gate' }
    format.js { render :json => { redirect: true } , :status => :unauthorized }
  end

end # is_authorized

and some javascript:

$.ajaxSetup({
    statusCode: {
      401: function(data) {
        var response = JSON.parse(data.responseText);
        if (response.redirect == true) {
          location.href = "/gate";
        }
      }
    }
  });

So all I really needed to do was send a 401 back when the session is expired, listen for 401s in AJAX calls, and finally redirect if we recieve a 401 and redirect parameter.

Andere Tipps

If you are using ajax calls, you should return a JS redirecting/reloading page; otherwise page won't go to the '/gate' URL

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top