# Redirect whole page from AJAX action that updates a div

**URL:** <https://www.ruby-forum.com/t/redirect-whole-page-from-ajax-action-that-updates-a-div/60170>\
**Category:** Rails\
**Created:** [May 10, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/redirect-whole-page-from-ajax-action-that-updates-a-div/60170 "2006-05-10T21:15:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [May 10, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/redirect-whole-page-from-ajax-action-that-updates-a-div/60170/1 "2006-05-10T21:15:12Z")

</div>

Hi,

I am calling an action via AJAX to update a div. The action needs to  
redirect the page in under certain circumstances. However, if I just use  
“redirect\_to” in the action then of course what happens is that the page  
I want to redirect to replaces only the div, not the entire page, and I  
get messy nested layouts.

Is there a way around this? Can I redirect the entire document from an  
action that is called via AJAX to update a div?

Thanks for any help!  
Ingo W.

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [May 10, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/redirect-whole-page-from-ajax-action-that-updates-a-div/60170/2 "2006-05-10T21:45:48Z")

</div>

I’m assuming you’re using link\_to\_remote in which case you need to pass  
into that method a callback javascript function. Take a look at  
[http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000412](http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000412).  
You probably need to do something like the following:

link\_to\_remote 'My Link,  
:url =\> { :action =\> “action” },  
404 =\> “alert(‘Not found…? Wrong URL…?’)”,  
:failure =\> “window.location = request”

then when a redirect (302) is sent the failure handler will be hit. You  
probably want more error and bounds checking though.

I’m pretty sure this is clost to right and would write more but I need  
to run.

-Matt Bauer

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [May 11, 2006, 12:10am UTC](https://www.ruby-forum.com/t/redirect-whole-page-from-ajax-action-that-updates-a-div/60170/3 "2006-05-11T00:10:36Z")

</div>

You can also conditionally render an .rhtml file that contains a script  
block something like:

PrototypeHelper will by default try to evaluate scripts in returned  
blocks.
