Is there a simple example of me issuing an XMLHttpRequest and a rails app responding ... without all the Prototype overhead. All I want to do is send an XHR and have the Rails app deliver an XML document back. I have searched ... Does anyone nkow of a decent example? -- 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.
Ralph Shnelvar wrote:> Is there a simple example of me issuing an XMLHttpRequest and a rails > app responding ... without all the Prototype overhead.What are you considering overhead? XHR *is* fundamentally a JavaScript function...> > All I want to do is send an XHR and have the Rails app deliver an XML > document back.I would suggest using JSON instead of XML.> > I have searched ... Does anyone nkow of a decent example?There''s virtually nothing Rails-specific or Ajax-specific here. To the server, receiving an XHR is just like receiving any other request. What don''t you understand here? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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.
Hi Ralph, On Fri, May 21, 2010 at 7:09 PM, Ralph Shnelvar <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Is there a simple example of me issuing an XMLHttpRequest and a rails > app responding ... without all the Prototype overhead. > > All I want to do is send an XHR and have the Rails app deliver an XML > document back. > > I have searched ... Does anyone nkow of a decent example? >Depending on the UI element you want to use to trigger the ajax request, check out the rails documentation for: link_to_remote button_to_remote form_remote_for form_remote_tag Each helper will generate the appropriate Ajax request, using Prototype as a default. Rails does the coding for you. Check back if you have questions specific to one of those once you decide which is most appropriate for your situation. HTH, Bill -- 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.
Bill Walton wrote:> Hi Ralph, >> Each helper will generate the appropriate Ajax request, using Prototype > as a > default. Rails does the coding for you. Check back if you have > questions > specific to one of those once you decide which is most appropriate for > your > situation.Ok ... I need to do polling of the server. The polling will happen out of jQuery ... so there is no UI trigger event as such. Once I get my results back, I''ll use jQuery to manipulate the DOM to pupulate certain fields ... as indicated by the XML returned. Where I am getting confused is what on the server side picks on the request? What on the server side sends back status codes 1, 2, 3, and 4? -- 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.
Hi Ralph, On Fri, May 21, 2010 at 8:27 PM, Ralph Shnelvar <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> > > I need to do polling of the server.Then use the periodically_call_remote helper> The polling will happen out of jQuery ...Doesn''t really matter which library you''re using. The Rails helper will generate the call you need.> so there is no UI trigger event as such. >Right.> > Once I get my results back, I''ll use jQuery to manipulate the DOM to > pupulate certain fields ... as indicated by the XML returned. >Why? Rails will generate the html, probably a partial, that you need if you let it.> Where I am getting confused is what on the server side picks on the > request? What on the server side sends back status codes 1, 2, 3, and > 4? >I understand your confusion. You''re coming at this from a client-side processing model. Rails is server-side technology. You send it a request and it sends back the html / js that you need. As you get started with Rails it pays to let go and let Rails do it for you. If I understand your situation, you''ll use periodically_call_remote to issue a request to your Rails app. Based on the state of something, you''ll render a partial updating your page. RJS templates are what you''re looking for. Cody Fauser''s pdf document with the same title at O''Reilly is the best $10 bucks you''ll ever spend on this. HTH, Bill -- 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 May 22, 2:27 am, Ralph Shnelvar <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Bill Walton wrote:> Ok ... > > I need to do polling of the server. The polling will happen out of > jQuery ... so there is no UI trigger event as such. > > Once I get my results back, I''ll use jQuery to manipulate the DOM to > pupulate certain fields ... as indicated by the XML returned. > > Where I am getting confused is what on the server side picks on the > request? What on the server side sends back status codes 1, 2, 3, and > 4?if by 1/2/3/4 you mean the XMLHttpRequest''s readyState, nothing on the server manipulates that directly - the browser''s implementation of XMLHttpRequest changes the readyState as the process of making the request advances. From the point of view of your rails app, this is just another request - you can render html/json/xml just as you would for any request. You may want to use respond_to or xhr? to adapt your response to the fact that it is an ajax request if your action is one that can be used by non ajax clients 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.
> RJS templates are what you''re looking for. Cody Fauser''s pdf document > with > the same title at O''Reilly is the best $10 bucks you''ll ever spend on > this. > > HTH, > BillIt''s a terrific article that is quite out-of-date. Now I have to struggle to update the deprecated stuff. -- 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.
Ralph Shnelvar wrote:> >> RJS templates are what you''re looking for. Cody Fauser''s pdf document >> with >> the same title at O''Reilly is the best $10 bucks you''ll ever spend on >> this. >> >> HTH, >> Bill > > It''s a terrific article that is quite out-of-date. > > Now I have to struggle to update the deprecated stuff.Or skip the RJS and just write JavaScript. I tend to think that RJS is one of the more pointless features of Rails... Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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.
> Or skip the RJS and just write JavaScript. I tend to think that RJS is > one of the more pointless features of Rails... > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.orgWhich is exactly what I have been trying to do! But I _still_ can''t find a decent tutorial that explains how to send an XHR request to a RoR app and what to do on the server side to process the request. -- 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 Mon, May 24, 2010 at 11:38 AM, Ralph Shnelvar <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> But I _still_ can''t find a decent tutorial that explains how to send an > XHR request to a RoR app and what to do on the server side to process > the request.? "AJAX tutorial" turns up quite a list on Google... There''s no reason an explanation of the client-side process needs to reference RoR in any way -- on the server side, it''s just another request. Though googling "AJAX rails tutorial" also turns up a long list :-) -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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 May 24, 2010, at 11:38 AM, Ralph Shnelvar wrote:> >> Or skip the RJS and just write JavaScript. I tend to think that RJS is >> one of the more pointless features of Rails... >> >> Best, >> -- >> Marnen Laibow-Koser >> http://www.marnen.org >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > Which is exactly what I have been trying to do! > > But I _still_ can''t find a decent tutorial that explains how to send an > XHR request to a RoR app and what to do on the server side to process > the request.Ralph, you are looking for two different kinds of knowledge: 1. How to send an XMLHttpRequest. This is a jQuery question. The answer is usually jQuery.post or jQuery.ajax. You will have to include your authentication token in the post request if you are protecting against forgery. 2. How Rails responds to the XMLHttpRequest. This is simply handled by a controller action. Use something like if xhr? to make sure you are responding to an XMLHttpRequest if that''s important to you. 3. How to render a response that you can then use to change the page. This is simply whatever you put in the response body. As someone previously noted, JSON may be easier and more flexible to use than XML. Then couple this with the success() callback in your jQuery .post or .ajax to change your DOM appropriately. This is not a cookbook solution, as all such applications are different. Hopefully, it''s a starting point for your research and you''ll find it easy to join Rails and jQuery. Steve -- 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.
If you''re set on doing the ajax through jQuery try adding this to your page and watch the console logs in firebug (obviously replacing ''posts and ''index'' with the route to the model and action you''re polling): // poll the posts index every 3000 milliseconds logging the response to the console. setInterval(function() { jQuery.getJSON(''/posts/index'', function(data) { console.log(data); }); }, 3000); This is a pretty crude approach but should illustrate the basics of getting jquery talking to rails. This does make a request for JSON but there really isn''t any reason you should be using XML over JSON at this point. Hope that helps. On May 21, 5:09 pm, Ralph Shnelvar <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Is there a simple example of me issuing an XMLHttpRequest and a rails > app responding ... without all the Prototype overhead. > > All I want to do is send an XHR and have the Rails app deliver an XML > document back. > > I have searched ... Does anyone nkow of a decent example? > -- > Posted viahttp://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 athttp://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.