This is actually quite simple to implement. Add a hidden field to the nest_form field with the position attribute (add this attr to your model, of course) and add this to your js along with jquery-ui.js
$('#task_fields').sortable({ items: '.fields', dropOnEmpty: true, cursor: 'move', handle: '.move', opacity: 0.4, scroll: true, axis:'y', placeholder: 'ui-state-highlight', start: function(e, ui) { ui.placeholder.height(ui.item.height()); }, update: function() { $(this).children(".fields").each(function(index) { $(this).children('input.task_position').val(index + 1); }); } });
this is what i have in my _form.html.haml
= f.fields_for :tasks do |t| = t.text_field :name = t.hidden_field :position, :class => :task_position = t.collection_select :account_id, Account.all, :id, :full_name, :prompt => 'Assign task to...' .button-group %span{:class => "button icon no-text move pill"} = t.link_to_remove "", :class => "button icon no-text remove pill" = f.link_to_add "Add a Task", :tasks, :class => "button icon add"
It worked very well.
source share