Supporting Ajax with Rails

There are many different ways to provide Ajax support from Rails. Some use Rails itself as a foundation, a provider of web services, while others more tightly bind the Rails application to the details of the client interface.

Rails as a Server API

If a manager comes running down the hallway shouting that you should scrap everything you’ve built because he’s going to replace it with an Ajax application, you may, if you’ve built your application using Rails’ RESTful features, remain pretty calm. If you’ve built a consistently RESTful API, then Ajax applications issue their requests to it without you needing to tear it down and start over. Some refactoring to let the Ajax application grab smaller pieces might be a good idea, but overall you’ll have a solid foundation.

Note

The RESTful foundation is solid enough, in fact, that anyone who has access to your site could potentially build an Ajax application on top of it. While JavaScript limits XmlHTTPRequest calls to the same server the script came from, anyone who wants to can build a mashup combining your data with their interface if they’re willing to set up some creative HTTP proxying.

That doesn’t mean the job is done, of course—it just means that most of what you’ve built will work. You can focus on the clearly Ajax bits, mostly in your views, rather than having to sort through a huge mess of code that used to generate one kind of HTML data structure and trying to figure out how to make it generate another kind of HTML data structure. And if you’re panicking now because you didn’t build your application in complete—or any—conformance to REST, don’t worry. Rails’ strict separation between model, view, and controller still spares you work on the model level and below. Instead of having to tear everything down to the database level and start over, you probably have some controller logic and views to reexamine.

Whenever you’re developing a Rails application, it’s wise to think of the controller structure as an API. Is that API flexible enough to share data outside of your own custom web pages? Is that API clear enough that others can figure it out? Is that API reliable enough that other applications can use it without surprises? Is it stable enough? (Two of the nicest features of REST are consistency and predictability, but you can reach them other ways.)

An API isn’t nearly the whole story, though. While you can write an Ajax application in JavaScript on top of Rails’ RESTful APIs, it isn’t the easiest way to do it—and all that JavaScript takes it beyond the bounds this book can cover. If you want to explore writing Ajax applications by hand, you might explore Adding Ajax (O’Reilly, 2007) or Ajax: The Definitive Guide (O’Reilly, 2008).

Rails and the Client

Even though it’s a server-side framework, Rails offers a variety of features you can use to create client-based Ajax applications from within Rails, avoiding the challenge of writing JavaScript by hand:

Helper methods

Rails’ helper methods go well beyond the basic HTML creation, providing support for building more exciting DHTML and Ajax interfaces.

Integration with Prototype and Script.aculo.us

The Prototype library is a very simple JavaScript library that makes common tasks simpler, like making XmlHTTPRequest calls and retrieving nodes in documents. Script.aculo.us builds on Prototype and provides a wide variety of graphic effects that make it much easier to build a visual interface users can interact with. (Don’t like Prototype and Script.aculo.us? There are other options, though they’ll take some extra work. For one example, see jRails, which integrates jQuery with Rails, such as at http://ennerchi.com/projects/jrails.)

RJS

Remote JavaScript (RJS) is the jewel in Rails’ crown when it comes to Ajax development. RJS templates provide a way for developers to specify in Ruby what they want to have happen to the HTML document in the browser. Rails handles all the necessary conversion from the Ruby in the RJS template to the JavaScript actually used by the browser to implement the change.

If you’re going to be building Ajax applications specifically with Rails, and everyone involved in the interface development is comfortable working in a Rails environment (rather than the usual Ajax JavaScript client-side approach), using these tools can make it easier to build an Ajax application quickly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset