Sunday, September 03, 2006

UJS - Unobtrusive Javascript for Ruby on Rails is a very interesting new plugin for Rails that lets you put behaviours right in your Rails code.

I was starting to use the behaviour javascript library for behaviours, basically what this lets you do is to assign behaviours to HTML elements using CSS selectors, but this new UJS looks like it might be a little more straightforward. Instead of having to add the behaviours by hand to an external Javascript file, you can just add them to your Rails views and UJS will make the Javascript file for you.

I'm torn, I kind of like the control that writing your own Javascript file gives you, but on a recent project to write a website that allowed an artist to arrange her photos by dragging them around, I switched from writing my own .js file with calls to Scriptaculous to using the Rails helper "sortable_element".
Using sortable_element took 30 lines of Javascript code down to 3 lines of Ruby code, which was pretty sweet.