# Can I do a redirect in an ajax request?

**URL:** <https://www.ruby-forum.com/t/can-i-do-a-redirect-in-an-ajax-request/56634>\
**Category:** Rails\
**Created:** [April 2, 2006, 12:51am UTC](https://www.ruby-forum.com/t/can-i-do-a-redirect-in-an-ajax-request/56634 "2006-04-02T00:51:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 2, 2006, 12:51am UTC](https://www.ruby-forum.com/t/can-i-do-a-redirect-in-an-ajax-request/56634/1 "2006-04-02T00:51:07Z")

</div>

I’ve got a form on my page, and when the user submits it, sometimes  
I’d like to update an element on my page, and sometimes I’d like to  
redirect the browser to another url entirely. However if I do  
redirect\_to, it just updates the element with the results of the  
redirect. Is it possible to do what I want, or will I need to use RJS  
to do this?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 2, 2006, 1:03am UTC](https://www.ruby-forum.com/t/can-i-do-a-redirect-in-an-ajax-request/56634/2 "2006-04-02T01:03:21Z")

</div>

you don’t have to use RJS, but RJS is probably the simplest way.

rjs ex:

page.redirect\_to :controller =\> …, :action =\> …, …

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 2, 2006, 1:06am UTC](https://www.ruby-forum.com/t/can-i-do-a-redirect-in-an-ajax-request/56634/3 "2006-04-02T01:06:28Z")

</div>

Yeah I did some googling, and found that I could put this in the  
controller:

render :update do |page|  
page.redirect\_to home\_url  
end

However when I do that, the text “window.location.ref=…” shows up  
on the page. I don’t want the user to see that…maybe I can have it  
show a custom message when it performs the action instead or  
something?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [April 2, 2006, 1:18am UTC](https://www.ruby-forum.com/t/can-i-do-a-redirect-in-an-ajax-request/56634/4 "2006-04-02T01:18:47Z")

</div>

Okay once I took the update out of the form\_remote\_tag call, and just  
used page.replace\_html inside my controller, everything works nicely  
and doesn’t show the JS in the page anymore. Thanks Chris!

Pat
