XMLHttpRequest and Rails

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?

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,

Hi Ralph,

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

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?

Hi Ralph,

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

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

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.

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,

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@marnen.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.

? "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 :slight_smile:

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

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.