# Ajax and redirect

**URL:** <https://www.ruby-forum.com/t/ajax-and-redirect/189717>\
**Category:** Rails\
**Created:** [June 6, 2010, 2:26am UTC](https://www.ruby-forum.com/t/ajax-and-redirect/189717 "2010-06-06T02:26:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [June 6, 2010, 2:26am UTC](https://www.ruby-forum.com/t/ajax-and-redirect/189717/1 "2010-06-06T02:26:36Z")

</div>

Here is my use case

I have a search model with two actions search\_set and search\_show.

1 - A user loads the home page which contains a search\_form, rendered  
via a partial (search\_form).

2 - User does a search, and the request goes to search\_set, the search  
is saved and a redirect happens to search\_show page which again  
renders the search\_form with the saved search preferences. This search  
form is different than the one if step1, because it’s a remote form  
being submitted to the same action (search set)

3 - Now the user does another search, and the search form is submitted  
via ajax to the search\_set action. The search is saved and executed  
and now I need to present the result via rjs templates (corresponding  
to search\_show). I am told that if the request is xhr then I can’t  
redirect to the search\_show action? Is that right? If yes, how do I  
handle this?

Thanks

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [June 6, 2010, 12:26pm UTC](https://www.ruby-forum.com/t/ajax-and-redirect/189717/2 "2010-06-06T12:26:27Z")

</div>

Hi,

What you cannot do is mixing Ajax requests with full URL page requests

But what you can do is something like:

format.js {  
render :action=\> search\_show, :layout=\>false  
}

Your search\_show view will be rendered without any application layout  
provided you’re using the same content @variables…

Jan

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [June 9, 2010, 8:30am UTC](https://www.ruby-forum.com/t/ajax-and-redirect/189717/3 "2010-06-09T08:30:07Z")

</div>

With my search forms, I use two partials, one for the controls and one  
for the result and then just use a page update to redisplay both  
parts.

If you really want a redirect

On Jun 6, 11:25 am, javinto [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> format.js {  
> render :action=\> search\_show, :layout=\>false  
> }

javinto,  
Interesting, something I have not tried. Is this an ajax update  
response, in which case, the part of the dom being updated would  
presumably need to be the part generated by the yield in the layout?

badnaam  
I use ajax search forms and handle the update as two partials, one for  
the controls and one for the results, using  
render :update

Tonypm

---

<div class="post-metadata">

**Author:** ![badnaam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e36b37/32.png) [@badnaam](https://www.ruby-forum.com/u/badnaam)\
**Post date:** [June 10, 2010, 7:40pm UTC](https://www.ruby-forum.com/t/ajax-and-redirect/189717/4 "2010-06-10T19:40:09Z")

</div>

> to search\_show). I am told that if the request is xhr then I can’t  
> redirect to the search\_show action? Is that right? If yes, how do I  
> handle this?

```
Hi If what I understood from your post is correct, you can do from 

```

controller like

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

Sijo
