In the past, Rails shipped with a JS library called
RJS that, when utilized, would give access to helpers for things such as making AJAX calls. These JS functions were typically meant to interact with other JS generated via Rails helpers, such as
link_to_remote, from within your view files.
Before Rails 3.1
app/assets directory and are cached, compressed and compiled as necessary. The power behind this approach is explained well in the official Rails Guide and is powered by Sprockets.
jQuery UJS /
jquery-ujs when using jQuery, or
rails.js when using Prototype. It is included into your layout by default.
data-method and manipulates them.
The most common example is when, in our view template, we’ve written:
The generated HTML looks like this:
- Wraps the element in a hidden HTML form
- Adds a click action to the link which will submit the form
- Injects a hidden form input named
_methodwith the value specified by
This way, when the link is clicked:
- An HTTP
POSTsubmits the form
- The router recognizes the special
_methodparameter and pretends the incoming request is using the verb specified
- The router will trigger the
articles#destroypath based on the path and
Confirmation dialogs are handled similarly. When we write this in our view template:
1 2 3
We get out HTML like this:
- Clicking the link will pop up a JS confirmation dialog with the message supplied by the
OKwill follow the link specified
CANCELwill return to the page with no changes
Use the Blogger sample application to complete the exercises in this section. See the Setup Instructions for help.
- Go to the
showpage for an Article and inspect the delete link. What markers do you see embedded there?
- Try changing the
GETusing your browser’s source navigator, then click the link. What happens?
- Try #2 again, but with a bogus verb like
HOWDY. What happens when you click the link?
- Experiment with adding a
:confirmparameter to a link that is not a delete. Does it work?
- jQuery UJS Adapter for Rails: https://github.com/rails/jquery-ujs