Tutorial :How to create a sortable interface with 'acts as nested set' in RubyOnRails

Question:

I've been implementing some nice interactive interfaces that can sort lists in my m rails app for models that use acts_as_list. I have a sort function that gets called and sets the position for each record afterr each drag and drop using the sortable_element script.aculo.us function.

This is an example of the controller action that handles the sort after the drag and drop completes:

I am stuck on how to write the controller action to handle the sort when the drag and drop completes.

I've added the :tree=>true parameter to the sortable _element function so far which appears to send a list of hashes but it seems that I am still missing information about the entire nested order....

I was certain this has been done before and didn't want to try to reinvent the wheel, but I can't seem to find any examples of the controller action <-> view with js function setup to handle a sortable acts_as_nested_set

Any help with creating an interactive sortable nested set in rubyonrails would be appreciated!

Solution:2

It's a hacky way of doing it, but its basically, sort first, then save order. Uses nestedsortables, serializelist, and 2 actions to traverse the tree

PS: I know this question is over a year old but hoping that the link above helps someone else coming here.

edit: added Rails3 example with some slightly cleaner code.

Solution:3

Just found this:

sortable_element_for_nested_set on github

Looks like it'll do the job, however I'm having some bugs while trying to implement it. It basically makes the javascript return the id of the element that was moved, then goes through the elements and returns its new parent, left and right values. Can't believe it's taken this long for something like this to be written! Lucky it was just when I needed it :)