Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Performing a redirect with AJAX (See related posts)

If a redirect is returned from the rails' form_remote_tag or link_to_remote methods, a message is displayed saying something like "You are being redirected", where "redirected" is a link the user must click. The code snippet below will actually redirect the user to the proper location, without them having to click the link. Code works for both form_remote_tag or link_to_remote methods.

<% link_to_remote "Foo",
                  :url => { :action => "foo" },
                  302 => "document.location = request.getResponseHeader('location')" %>



Or, an example from the Typo source:
<%= form_remote_tag :url => {:action => "comment", :id => @article}, 
                    :update => {:success => 'commentList'},
                    :loading => "loading()", 
                    :complete => "complete(request)",
                    :failure => "failure(request)",
                    :html => {:id=>"commentform",:class=>"commentform"},
	            302 => "document.location=request.getResponseHeader('location')"%>

Comments on this post

Chewi posts on Dec 05, 2006 at 14:16
It's a nice idea but it doesn't work for me. Instead, the redirect happens within the element I'm trying to update so I get a "site within a site" effect. Maybe Prototype was updated to handle 302 itself?

You need to create an account or log in to post comments to this site.


Click here to browse all 5059 code snippets

Related Posts