sortable_element, acts_as_list, and GUID plugin
Sunday, April 20th, 2008Briefly, I just wanted to jot down some notes that might help some else working with any of these rails tools:
- sortable_element: This is a method in the ScriptaculousHelper, which will easily allow you to implement a drag and drop list.
- acts_as_list: A rails plugin that will manage an order list of relations. This will add move_up and move_down as methods in your model. It is quote convenient.
- GUID: A rails plugin that will turn your activerecord id into a URL-safe GUID.
In my attempts to use the three of these together, I found that the sortable_element format regex, which is used to extract the id from the id attribute of the li tag, was not acceptable for use with the GUID plugin. At first glance, it looks like it should have worked. Anyway, by trying to follow the documentation, I tried the following:
The document said the format was a regex, so I gave it a regex. This did not work! It creates invalid javascript.
<%= sortable_element('tasks', :url => {:action => 'update_positions'}, :ghosting => true, :format => /^task_(.*)$/, :only => 'task')%>
Ok so I regex does not work, I will try a string.
<%= sortable_element('tasks', :url => {:action => 'update_positions'}, :ghosting => true, :format => "^task_(.*)$", :only => 'task')%>
String does not work, so let’s try a javascript string.
<%= sortable_element('tasks', :url => {:action => 'update_positions'}, :ghosting => true, :format => "'^task_(.*)$'", :only => 'task')%>
As you can see my approach to problem solving is not methodical. It is more brute force.
The moral of the story is to use a javascript string for the format configuration for the sortable_element.