# Conditionally respond with HTTP redirect for xml\_http\_request

**URL:** <https://www.ruby-forum.com/t/conditionally-respond-with-http-redirect-for-xml-http-request/165010>\
**Category:** Rails\
**Created:** [April 8, 2009, 7:52am UTC](https://www.ruby-forum.com/t/conditionally-respond-with-http-redirect-for-xml-http-request/165010 "2009-04-08T07:52:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [April 8, 2009, 7:52am UTC](https://www.ruby-forum.com/t/conditionally-respond-with-http-redirect-for-xml-http-request/165010/1 "2009-04-08T07:52:18Z")

</div>

Hi all,

I have select box in one of my views which lists a few contacts.  
I also have an option in this select box to add a new contact.  
There is also an observe\_field hook to this select box that Ajax  
requests further info about the selected contact from the controller  
and displays it in an adjacent “info” div.

Now, I need to redirect the user to the New Contact page from the  
controller if the selected value from the select box is “new contact”.

This is the controller code I have now  
…  
if request.xml\_http\_request?  
if !@contact.blank?  
render :partial =\> “show”, :layout =\> false  
elsif params[:id] == “new contact”  
redirect\_to new\_contact\_path  
else  
render :text =\> “Please select a contact”  
end  
end

This renders the New Contact page in the “info” div. Whereas, I want  
the user to be completely redirected to the New Contact page like a  
HTTP response would.  
Any thoughts on how I can handle this? Any ideas welcome…  
Thanks!

---

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [April 8, 2009, 9:20am UTC](https://www.ruby-forum.com/t/conditionally-respond-with-http-redirect-for-xml-http-request/165010/2 "2009-04-08T09:20:51Z")

</div>

On Apr 8, 6:51 am, Ram [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> This renders the New Contact page in the “info” div. Whereas, I want  
> the user to be completely redirected to the New Contact page like a  
> HTTP response would.  
> Any thoughts on how I can handle this? Any ideas welcome…  
> Thanks!

Two ways of doing this: either the observe\_field thingy has a callback  
that redirects in the right circumstances or you switch to using  
render :update (and then use page.redirect\_to), In the latter case you  
have to use render :update for all of these 3 possibilities and you  
need to change your observe\_field invocation to not pass the :update  
option.

Fred

---

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [April 8, 2009, 9:39am UTC](https://www.ruby-forum.com/t/conditionally-respond-with-http-redirect-for-xml-http-request/165010/3 "2009-04-08T09:39:36Z")

</div>

Hi Fred,

Thanks for your response.

render :update  
Ive got quite a few Ruby conditions and styling going in the js.erb  
file im using now to display the contact information. And ive got more  
possibilities than the 3 ive shown above in the controller. So this  
might not be the right option for me. Right?

observe\_field callback  
This might work for me. Right now, the only callbacks I have on the  
observe\_field are showing and hiding the spinner. I have never written  
custom callbacks on Rails JS helpers. Could you help me through this?

So im guessing I should be having something like this

\<%= observe\_field ‘contact\_id’, :frequency =\> 0.5, :update =\>  
‘info’, :before =\> “Element.show(‘spinner’)”,  
:success =\> “handle\_contact\_request(value);”, :url =\>  
show\_contact\_path,:method =\>:get,  
:with =\> ‘contact\_id’ %\>

and in application.js

function handle\_contact\_request(value){

```
     Element.hide('spinner');
     if value=="new contact"
     {
            //how do i redirect to the New Contact page?
     }
     else
     {
           //show contact info. But how do I handle the conditions

```

and styling??  
}  
}

Am I getting the basic idea right? If yes, how can I handle the  
conditional redirecting and the rest in Javascript?  
Thanks again Fred.

On Apr 8, 12:20 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [April 8, 2009, 9:58am UTC](https://www.ruby-forum.com/t/conditionally-respond-with-http-redirect-for-xml-http-request/165010/4 "2009-04-08T09:58:57Z")

</div>

On Apr 8, 8:38 am, Ram [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Fred,
> 
> Thanks for your response.
> 
> render :update  
> Ive got quite a few Ruby conditions and styling going in the js.erb  
> file im using now to display the contact information. And ive got more  
> possibilities than the 3 ive shown above in the controller. So this  
> might not be the right option for me. Right?

I’m not sure this would be a problem - seems like a few calls to  
page.replace\_html ‘info’, :partial =\> ‘blah’ would do it in the other  
cases.

> ```
> :success => "handle_contact_request(value);", :url =>
> 
> ```
> 
> show\_contact\_path,:method =\>:get,  
> :with =\> ‘contact\_id’ %\>

Nearly. your success callback gets 2 thigns: response and  
responseJSON. responseJSON contains whatever JSON you put in the X-  
JSON header of your response. You callback can be as simple as

check\_for\_redirect(json){  
if(json && json.redirect)  
page.location = json.redirect  
}

and then pass :success =\> “check\_for\_redirect(responseJSON)”. All you  
need to do is to set that header appropriately if you want a redirect  
to happen.

Fred

---

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [April 8, 2009, 10:47am UTC](https://www.ruby-forum.com/t/conditionally-respond-with-http-redirect-for-xml-http-request/165010/5 "2009-04-08T10:47:40Z")

</div>

🙂 You were right. It was easy enough to use render :update and  
replace\_html with :partial to accomplish it.

I am however curious as to how we can do this using JSON. I started  
reading about JSON for another feature I needed but I realised I dint  
need JSON for it. I feel it would be a good exercise for me to  
implement it in tackling this problem. Could you explain a little bit  
more about the code you have shown me above or point me to where I can  
find the documentation explaining this? From what I remember, JSON is  
a format (Object Notation) to represent ActiveRecord objects (in  
Rails) for JS to handle. But setting the header, responseJSON et all  
is new to me.

Thanks Fred!

On Apr 8, 12:57 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@ram](https://www.ruby-forum.com/u/ram)\
**Post date:** [April 8, 2009, 10:58am UTC](https://www.ruby-forum.com/t/conditionally-respond-with-http-redirect-for-xml-http-request/165010/6 "2009-04-08T10:58:15Z")

</div>

I am trying to implement Craig A.'s Redbox for some form  
submissions.  
Ideally for me, when the user selects “Add contact” a redbox modal  
window should open with the New Contact form and on submission, the  
contacts select box on the main page should get updated with the newly  
added contact.  
I was looking into invoking Redbox from the controller for this  
([http://ahref.in/8dc58](http://ahref.in/8dc58)) but I couldnt quite grasp the discussion  
there.  
Can Redbox be brought in here without too much trouble?  
thanks again for you inputs!
