Thomas Fuchs
Hi, I'm Thomas Fuchs. I'm the author of the script.aculo.us user interface JavaScript library, a member of the Prototype core team and a Ruby on Rails core alumnus.
You're using my JavaScript work every day, even if you're not aware of it!
@thomasfuchs on Twitter    Need consulting, corporate training or want me to speak at your conference? Contact me!

AJAX-enabled sortable elements in Rails, the movie

July 1st, 2005

To sweeten the waiting time to the next Rails release, here’s a short movie showing the new sortable_element helper in action:

AJAX sortable element movie

The code for this is actually a one-liner (including the effect). In your view do:


<%= sortable_element 'list',
      :update => 'list-info',
      :complete => visual_effect(:highlight, 'list'),
      :url => { :action => "order" } %>

The list element is just a normal HTML unordered list (but you could do this with other element types, too). The list items have ids themselves, in the form of something_number, e.g. item_1.

When the list order changes by dragging-and-dropping, these numbers will be posted to the given action via AJAX.

The referenced action order then just reads out the order by querying params[:list], which is an array consisting of the numbers in the current order.