# Render an action if coming from ajax request

**URL:** <https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814>\
**Category:** Rails\
**Created:** [June 8, 2010, 10:20am UTC](https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814 "2010-06-08T10:20:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 8, 2010, 10:20am UTC](https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814/1 "2010-06-08T10:20:46Z")

</div>

Hi,  
I have a Rails web app working, and I am now working on a mobile app  
using Phonegap. Phonegap works only with javascript/html, so I make Ajax  
request to my web app and render things if the request is make from  
Ajax.

This is what I have in my controller action  
if request.xhr?  
render :controller =\> ‘user\_session’, :action =\> ‘new’, :layout =\>  
false params[:id].present?  
render :controller =\> ‘user’, :action =\> ‘new’, :layout =\> false if  
params[:id].blank?  
else  
redirect\_to login\_path if params[:id].present?  
redirect\_to register\_path if params[:id].blank?  
end

My javascript  
HOST = ‘localhost:3000’;  
new Ajax.Request(HOST,  
{  
method: ‘get’,  
onComplete: function(transport){alert(‘kikoo’);}  
}  
);

I have two problem with this code, the first one is that it doesn’t  
recognize that I am making an Ajax request because it is going in the  
else condition. And the second problem is that if I remove the if  
statement I have rendering problem, it tries to render the new action  
from the current controller.

Thanks  
Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 8, 2010, 10:44am UTC](https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814/2 "2010-06-08T10:44:35Z")

</div>

On 8 June 2010 09:20, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> false params[:id].present?  
> There is an ‘if’ missing above

Colin

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 8, 2010, 10:46am UTC](https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814/3 "2010-06-08T10:46:25Z")

</div>

Colin L. wrote:

> On 8 June 2010 09:20, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > false params[:id].present?  
> > There is an ‘if’ missing above
> 
> Colin  
> I fixed that, consider the ‘if’ is there

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 8, 2010, 11:03am UTC](https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814/4 "2010-06-08T11:03:19Z")

</div>

On 8 June 2010 09:20, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> false params[:id].present?  
> {  
> method: ‘get’,  
> onComplete: function(transport){alert(‘kikoo’);}  
> }  
> );
> 
> I have two problem with this code, the first one is that it doesn’t  
> recognize that I am making an Ajax request because it is going in the  
> else condition

Have you looked in the log to see what the request is? Have you  
broken in to the code using ruby-debug for example to check the value  
of request.xhr?

> . And the second problem is that if I remove the if  
> statement I have rendering problem, it tries to render the new action  
> from the current controller.

Again does the log show anything useful?

Colin

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 8, 2010, 11:15am UTC](https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814/5 "2010-06-08T11:15:16Z")

</div>

Colin L. wrote:

> On 8 June 2010 09:20, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > false params[:id].present?  
> > {  
> > ï¿½method: ‘get’,  
> > ï¿½onComplete: function(transport){alert(‘kikoo’);}  
> > }  
> > );
> > 
> > I have two problem with this code, the first one is that it doesn’t  
> > recognize that I am making an Ajax request because it is going in the  
> > else condition
> 
> Have you looked in the log to see what the request is? Have you  
> broken in to the code using ruby-debug for example to check the value  
> of request.xhr?
> 
> > . And the second problem is that if I remove the if  
> > statement I have rendering problem, it tries to render the new action  
> > from the current controller.
> 
> Again does the log show anything useful?
> 
> Colin

Yes I have check the log and it doesn’t help. I logged each step and I  
confirm that it is not an Ajax request…

Processing HomeController#index (for 127.0.0.1 at 2010-06-08 11:13:56)  
[OPTIONS]  
Parameters: {“action”=\>“index”, “id”=\>“”, “controller”=\>“home”}  
#ActionController::Request:0x7fd5dcc83b88  
no current user no xhr  
Redirected to [http://localhost:3000/register](http://localhost:3000/register)  
Completed in 15ms (DB: 0) | 302 Found [[http://localhost/?id=](http://localhost/?id=)]

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 8, 2010, 11:29am UTC](https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814/6 "2010-06-08T11:29:40Z")

</div>

On Jun 8, 10:15 am, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yes I have check the log and it doesn’t help. I logged each step and I  
> confirm that it is not an Ajax request…

Rails looks at the X-Requested-With header to see if a request was an  
ajax one. Prototype, jquery etc. set this header, but if phonegap  
doesn’t then rails won’t consider it to be an ajax request

Fred

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 8, 2010, 11:37am UTC](https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814/7 "2010-06-08T11:37:50Z")

</div>

Frederick C. wrote:

> Rails looks at the X-Requested-With header to see if a request was an  
> ajax one. Prototype, jquery etc. set this header, but if phonegap  
> doesn’t then rails won’t consider it to be an ajax request
> 
> Fred

I’m not making the Ajax query with phonegap but with prototype

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 8, 2010, 10:24pm UTC](https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814/8 "2010-06-08T22:24:53Z")

</div>

I don’t understand, I’ve been testing on Firefox and now I am on Safari  
and it works…  
Someone has an idea why?

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 8, 2010, 4:21pm UTC](https://www.ruby-forum.com/t/render-an-action-if-coming-from-ajax-request/189814/9 "2010-06-08T16:21:13Z")

</div>

Frederick C. wrote:

> On Jun 8, 10:15ï¿½am, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Yes I have check the log and it doesn’t help. I logged each step and I  
> > confirm that it is not an Ajax request…
> 
> Rails looks at the X-Requested-With header to see if a request was an  
> ajax one. Prototype, jquery etc. set this header, but if phonegap  
> doesn’t then rails won’t consider it to be an ajax request
> 
> Fred

X-Requested-With header looks like this when I execute the Ajac in  
firebug:  
Access-Control-Request-He… x-prototype-version,x-requested-with
