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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 8 June 2010 09:20, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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?There is an ''if'' missing above Colin> 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 > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote:> On 8 June 2010 09:20, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> false params[:id].present? > There is an ''if'' missing above > > ColinI fixed that, consider the ''if'' is there -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 8 June 2010 09:20, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 conditionHave 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote:> On 8 June 2010 09:20, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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? > > ColinYes 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=] -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jun 8, 10:15 am, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 > > FredI''m not making the Ajax query with phonegap but with prototype -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote:> On Jun 8, 10:15�am, Greg Ma <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > > FredX-Requested-With header looks like this when I execute the Ajac in firebug: Access-Control-Request-He... x-prototype-version,x-requested-with -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I don''t understand, I''ve been testing on Firefox and now I am on Safari and it works... Someone has an idea why? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.