# How to make an ajax call to redirect

**URL:** <https://www.ruby-forum.com/t/how-to-make-an-ajax-call-to-redirect/151063>\
**Category:** Rails\
**Created:** [October 16, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/how-to-make-an-ajax-call-to-redirect/151063 "2008-10-16T13:29:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [October 16, 2008, 1:29pm UTC](https://www.ruby-forum.com/t/how-to-make-an-ajax-call-to-redirect/151063/1 "2008-10-16T13:29:47Z")

</div>

I have an ajax call method like following:  
##############################  
def find\_created\_actions  
unless @if\_login  
flash[:login]=“ÇëµÇÂ¼»ò×¢²á” redirect\_to(:controller=\>“login”,:action=\>“login”)  
end  
if @if\_login  
@user\_cactions=@user.sponsored\_actions  
render :partial=\>“created\_actions”,:object=\>@user\_cactions  
end  
end  
###################################  
I hope when some condition met,the call will let some partial replace  
the html element’s  
content,this works now.When the condition is not met,i hope redirect  
to another  
controller’s action,not replace something.but in fact,that action’s  
template replace  
the html element’s content,this is not the result i want.How do i  
manage this?

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [October 18, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/how-to-make-an-ajax-call-to-redirect/151063/2 "2008-10-18T22:31:26Z")

</div>

On Thu, Oct 16, 2008 at 1:24 AM, daociyiyou [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> end  
> ###################################  
> I hope when some condition met,the call will let some partial replace  
> the html element’s  
> content,this works now.When the condition is not met,i hope redirect  
> to another  
> controller’s action,not replace something.but in fact,that action’s  
> template replace  
> the html element’s content,this is not the result i want.How do i  
> manage this?

Instead of redirect\_to render a partial with a line of JS like this:

## HTH,

Hassan S. ------------------------ removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [October 18, 2008, 11:49pm UTC](https://www.ruby-forum.com/t/how-to-make-an-ajax-call-to-redirect/151063/3 "2008-10-18T23:49:58Z")

</div>

Hi daociyiyou ,

Try this:

def find\_created\_actions  
unless @if\_login  
flash[:login]="…"  
redirect\_to(:controller=\>“login”,:action=\>“login”)  
end  
if @if\_login  
@user\_cactions=@user.sponsored\_actions  
render :update do |page|  
page.replace\_html ‘element\_id’,  
:partial =\> “created\_actions”, :object =\> @user\_cactions  
end  
else  
render :update do |page|  
page.redirect\_to(:controller=\>“login”,:action=\>“login”)  
end  
end  
end

And make sure you’re _not_ using the :update option on your remote\_form.  
‘page.redirect\_to()’ will generate javascript similar to Hassan’s  
aforementioned suggestion.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [September 24, 2010, 4:21am UTC](https://www.ruby-forum.com/t/how-to-make-an-ajax-call-to-redirect/151063/4 "2010-09-24T04:21:05Z")

</div>

Sjoerd A. wrote:

> Hi daociyiyou ,
> 
> Try this:
> 
> def find\_created\_actions  
> unless @if\_login  
> flash[:login]=“…”  
> redirect\_to(:controller=\>“login”,:action=\>“login”)  
> end  
> if @if\_login  
> @user\_cactions=@user.sponsored\_actions  
> render :update do |page|  
> page.replace\_html ‘element\_id’,  
> :partial =\> “created\_actions”, :object =\> @user\_cactions  
> end  
> else  
> render :update do |page|  
> page.redirect\_to(:controller=\>“login”,:action=\>“login”)  
> end  
> end  
> end
> 
> And make sure you’re _not_ using the :update option on your remote\_form.  
> ‘page.redirect\_to()’ will generate javascript similar to Hassan’s  
> aforementioned suggestion.

Hi, just a note that this does not work. Because the redirect gets  
trapped inside the Ajax call. If you use firebug you can see what  
happens.

So it seems the only way to redirect is to use a javascript redirect as  
mentioned on:

> <https://stackoverflow.com/questions/1215177/cannot-do-response-redirect-from-page-with-ajax-controls>

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [October 20, 2008, 9:36am UTC](https://www.ruby-forum.com/t/how-to-make-an-ajax-call-to-redirect/151063/5 "2008-10-20T09:36:16Z")

</div>

On Oct 19, 5:49 am, Sjoerd A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> @removed_email_address@domain.invalid_actions
> 
> ```
> 
> ## And make sure you’re _not_ using the :update option on your remote\_form. ‘page.redirect\_to()’ will generate javascript similar to Hassan’s aforementioned suggestion.
> 
> Posted viahttp://www.ruby-forum.com/.

Thank you,it should work now.

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [September 24, 2010, 9:38am UTC](https://www.ruby-forum.com/t/how-to-make-an-ajax-call-to-redirect/151063/6 "2010-09-24T09:38:14Z")

</div>

Vicer Ontero wrote:

> > And make sure you’re _not_ using the :update option on your remote\_form.  
> > ‘page.redirect\_to()’ will generate javascript similar to Hassan’s  
> > aforementioned suggestion.
> 
> Hi, just a note that this does not work. Because the redirect gets  
> trapped inside the Ajax call. If you use firebug you can see what  
> happens.
> 
> So it seems the only way to redirect is to use a javascript redirect as  
> mentioned on:  
> [asp.net - Cannot do response.redirect from page with Ajax controls - Stack Overflow](http://stackoverflow.com/questions/1215177/cannot-do-response-redirect-from-page-with-ajax-controls)

You’re right. There’s a confinient helper to generate the right  
javascript  
([redirect\_to (ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods) - APIdock](http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods/redirect_to)):

def find\_created\_actions  
unless @if\_login  
render :update do |page|  
flash[:login]=“…”  
page.redirect\_to(:controller=\>“login”,:action=\>“login”)  
end  
end  
[…]  
end

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [October 1, 2010, 1:59am UTC](https://www.ruby-forum.com/t/how-to-make-an-ajax-call-to-redirect/151063/7 "2010-10-01T01:59:10Z")

</div>

Here is a cleaner way. Just put the following code on your  
application\_controller.rb.  
Now, redirect\_to should work with Ajax calls as well as regular  
requests.

def redirect\_to(options = {}, response\_status = {})  
if request.xhr?  
render(:update) {|page| page.redirect\_to(options)}  
else  
super(options, response\_status)  
end  
end

Cheers,  
Glen

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [October 29, 2010, 10:56pm UTC](https://www.ruby-forum.com/t/how-to-make-an-ajax-call-to-redirect/151063/8 "2010-10-29T22:56:07Z")

</div>

Glen’s solution works perfect !  
Thank you very much, your tip is a must have of every rails project,  
IMHO 🙂
