Forum: Ruby on Rails ajax redirection

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8987dd66d952d35845f19ae8f6d78291?d=identicon&s=25 Sebastien Hugues (Guest)
on 2006-03-24 11:48
(Received via mailing list)
Hi,

Is there a simple way to redirect towards another page (controller,
action)
when using form_remote_tag ?

I used it tipically to check fields of a account login form. I'd like
if the user
exists, he's redirected towards another page, otherwise I display some
error div's.

Any ideas ?
Thanks a lot.
Seb
7cda78d70ab775710f6fa24e0a5b68db?d=identicon&s=25 Emin Hasanov (Guest)
on 2006-03-24 11:59
(Received via mailing list)
You should put a callback in your tag like:

:complete => "redirect(request)"

and then add redirect() javascript function that will check
request.ResponseText and do a client-side redirect if it meets
required criteria

more on ajax callbacks here:
http://api.rubyonrails.com/classes/ActionView/Help...
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-24 16:51
(Received via mailing list)
if you are using rjs (plugin for 1.0 or built-in for 1.1), you can use
page.redirect_to

example:

/app/views/account/_login_form.rhtml

<%= form_remote_tag :url => { :action => :login } %>
...
<%= end_form_tag %>
<div id="login_msg"></div>

/app/controllers/account_controller.rb

class AccountController < ApplicationController
  def login
    # the prototype library sends a "X-Requested-With" header containing
"XMLHttpRequest"
    # so we can tell if it's an AJAX call or not
    if request.xhr?
      # authenticate the user
      @authenticated = User.authenticate(@params[:login],
@params[:password])
      # go on to render the login.rjs template
    else
      # not an ajax call, so we display the login form partial instead
      render :partial => "login_form", :layout => false
    end
  end
end

/app/views/account/login.rjs

if @authenticated
  # user has authenticated so send them to where they need to go
  page.redirect_to("/controller/action")
else
  # auth failed so update the message div to let them know
  page.replace_html("login_msg", "Bad username or password")
end
This topic is locked and can not be replied to.