8.6. Summary

Web 2.0 is largely powered by JavaScript via the asynchronous server call structure often called Ajax. Rails provides a number of different features that you can use to enable Ajax in your applications. The most basic structure is link_to_remote and remote_form_for helpers that enable an ordinary link or form to trigger an Ajax call and update an arbitrary part of the page. You can test these calls with the xhr functional test helper. You can also easily augment them to give users who don't have JavaScript some functionality.

You can add tooltips to your Rails applications with the Prototip library and a couple of simple helpers. Two former pieces of the Rails core, in-line editing and autocomplete text fields, are available as plugins and are still quite easy to use. You can also combine the Control.Modal library with some simple helpers to give a modal dialog, lightbox effect.

RJS allows you to write Ruby code that is converted to JavaScript, which enables you to use more complex effects than what you get with the base behavior of the remote helpers. RJS templates have a wide array of operators to manipulate the DOM of the client browser, including full access to the script.aculo.us effects library. You can use the Rails assert_select_rjs helper method or the ARTS plugin to test the result of an RJS call.

JavaScript applications can be vulnerable to cross-site scripting attacks. By using the sanitize helper, you can minimize the risk of such attacks dramatically. ...

Get Professional Ruby on Rails™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.