Rails 3.1 - Drag Drop Re-order without acts_as_list?

1.7k views Asked by At

I want to allow a drag and drop reorder facility within my app. I'm aware I can do this with acts_as_list, and have followed the Rails Recipe on this. However, due to the way my app is built, the item I want to reorder can't use acts_as_list as it inherits it's position field on create from nested items within another model. When I use acts_as_list, this seems to override the position value meaning the order set in the parent model is lost, child positions are then random.

So I suppose I have two questions:

Can I build an Ajax style drag and drop reorder facility without Acts_As_List?

or

Can I put some kind of if statement with the model to disable acts_as_list on create but enable at all other times? Something like (in english rather than code):

if method = create
  child.position = parent.position
else
  acts_as_list
end
3

There are 3 answers

0
pjammer On

I'm pretty sure jquery UI has some kind of plugin/module whatever they call them for this like draggable and sortable then maybe they have a success method that you can use ajax to record it's position so that if the user refreshes before a save, it saves it's position in the 'list'.

0
ohho On

ranked-model is an option. Example usage:

class Duck < ActiveRecord::Base

  include RankedModel
  ranks :row_order

end
0
zarazan On

You can just override the before_save function that is called in acts_as_list:

class Duck < ActiveRecord::Base

  private

  def add_to_list_bottom
    # leave blank to override default acts_as_list before_save function
  end

end