Tag Archive for 'jquery'

Unobtrusive RESTful links in Rails

In a recent project, I wanted a link that modifies a row in the database. The provided mockup wanted a link. “To follow the rules of RESTful architecture, anything modifying the database, should not be a GET request” I thought to myself. I realised that Rails provides me with both a link_to and a button_to helper but no hybrid that would be unobtrusive, so I knew I had to make one.

The snippet adds a link after the form, hides the form and make the link submit the form. I have made it non-conflicting to those who use other libraries and jQuery.

To install:

  • Use jRails or have the jQuery library included (Prototype not needed)
  • Add the snippet to your application.js
  • Create your button_to’s with the class :class => "form_to_link"

Thanks to Kyle Neath and Rein Henrichs with this.

7 Comments