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 S. 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’!


Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath

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!


Phlip

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

On Oct 4, 9:00 pm, Frederick C. [email protected]

On 4 Oct 2007, at 16:39, Phlip wrote:

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

On 10/4/07, Shishir S. [email protected] wrote:

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…??

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

unfotunately am getting this error

NameError
in LoginController#logout

undefined local variable or method `page' for
#<LoginController:0x7f2950c0>

RAILS_ROOT: script/../config/..

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

On 4 Oct 2007, at 21:55, Shishir S. wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs