Render an action if coming from ajax request

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

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?

There is an 'if' missing above

Colin

Colin Law wrote:

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

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

Colin Law 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 Completed in 15ms (DB: 0) | 302 Found [http://localhost/?id=]

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

Frederick Cheung 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

Frederick Cheung wrote:

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