8.2. Easy JavaScript Integration

Inserting JavaScript into your application need not be a major hassle. Many developers have worked hard to create packages in JavaScript and Rails to allow you to insert significant behavior with very little fuss. This section describes three easy ways to insert some very helpful JavaScript.

8.2.1. Tooltips

Users have come to expect tooltips (or "those things that float on the screen" as some users call them). Although many browsers will automatically overlay the alternate (alt) text from an image or link over those elements, you may want to include a more elaborate floating-text behavior. Once you have an easy way to display tooltips, having a layer of content on your application that is invisible except during mouseover (when the mouse hovers over a screen element) becomes an irresistible way of increasing the amount of content on your pages.

What I'd like to do, by way of example, is build an extremely thin Ruby layer around a JavaScript package called Prototip. Prototip is a tooltip library written by Nick Stakenburg on top of the Prototype JavaScript library. As I write this, Prototip is at version 1.1, and can be picked up at www.nickstakenburg.com/projects/prototip/. The zip file will contain a .js file, which you should put in your public/javascripts folder, and a .css (Cascading Style Sheets) file, which goes in public/stylesheets. To make these files available, you need to load them in the layout header. In app/views/layouts/recipes.html.erb ...

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.