JavaScriptHelper

Sometimes your Rails code will need to generate JavaScript, and not always in the context of RJS, as described in Chapter 16. These helper methods make it simpler to add basic JavaScript functionality to your pages, and remove the need to code some kinds of simple JavaScript by hand:

button_to_function

Creates a button that will call a JavaScript function using its onclick handler. It accepts a block of code, which works like RJS.

define_javascript_functions

Creates a link to Prototype and other JavaScript files, but is best avoided. Use javascript_include_tag instead.

escape_javascript

Reformats JavaScript containing carriage returns and quotes so that it can safely be put into HTML attribute values.

javascript_tag

Creates a script tag. Again, javascript_include_tag may be a better option.

link_to_function

Creates a link that will call a JavaScript function. Like button_to_function, it also can take a block that works like RJS.

Get Learning Rails: Live Edition 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.