Forum: Ruby on Rails Redirect whole page from AJAX action that updates a div

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.
3504745159d79c585e4f7df8df76aa10?d=identicon&s=25 Ingo Weiss (ingoweiss)
on 2006-05-10 21:15
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 Weiss
0d616af377890ddfad05cdee988146a9?d=identicon&s=25 Matt Bauer (mrbauer1)
on 2006-05-10 21:45
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/Help....
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
Fb23bc8cd4030c526b0689276b34c8bd?d=identicon&s=25 Bryan Duxbury (bryanduxbury)
on 2006-05-11 00:10
You can also conditionally render an .rhtml file that contains a script
block something like:

<script>
  document.location.href = "...";
</script>

PrototypeHelper will by default try to evaluate scripts in returned
blocks.
This topic is locked and can not be replied to.