redirect on AJAX request.

Hi,

Is there a way to redirect the page through a ajax request. I tried using link_to_remote tag as the logout link on a page and used the 'redirect_to index' in the called method. But I get the whole page in the request.reponseText as normal text and the browser page doesn't change.

Has anyone implemented this succesfully.?

TIA ~Shishir

Shishir Srivastava wrote:

Is there a way to redirect the page through a ajax request. I tried using link_to_remote tag as the logout link on a page and used the 'redirect_to index' in the called method. But I get the whole page in the request.reponseText as normal text and the browser page doesn't change.

Just send JS that sets the window.location:

  render :update do |rjs|     rjs << "window.location = #{ url_for something };"   end

I wrote 'rjs' where everyone else writes 'page' because that was a bad idea for a variable name. There are way too many variables out there called 'page'!

Just send JS that sets the window.location:

  render :update do |rjs|     rjs << "window.location = #{ url_for something };"

url_for(something).inspect, for free quotes and escapes!

page.redirect_to will do all that for you

Fred

a bit confused..

1. render :update do |rjs|     rjs << "window.location = #{ url_for something };"   end

2. page.redirect_to

which one of these is correct...or both of them are..??

~Shishir

They both are. #2 generates the same javascript as is shown in #1.

unfotunately am getting this error

  NameError    in LoginController#logout </h1> <pre>undefined local variable or method `page' for #&lt;LoginController:0x7f2950c0&gt;</pre> <p><code>RAILS_ROOT: script/../config/..</code></p>

i've added it like this in my LoginController#logout method

def logout session[:sess].sign_out page.redirect_to "index" end

where 'index' is my target page

~Shishir

unfotunately am getting this error

  NameError    in LoginController#logout </h1> <pre>undefined local variable or method `page' for #&lt;LoginController:0x7f2950c0&gt;</pre> <p><code>RAILS_ROOT: script/../config/..</code></p>

i've added it like this in my LoginController#logout method

def logout session[:sess].sign_out page.redirect_to "index" end

Read up on rjs. page is the name usually given to the instance of
javascript generator you use. If you have an rjs template it will be
defined in there. If you have a render :update then it is the thing
that is yielded to the block. Either plonk it in logout.rjs or do
some pike render :update do |page| ... end

Fred