On Jan 24, 2011, at 1:45 PM, robo wrote:
I’ve got a sortable list that you can add new elements to dynamically,
using the prototype helpers (http://api.rubyonrails.org/classes/
I want to be able to remove a selected item from the list dynamically
– presumably, each item in the list would have a link_to_remote that
would pop a confirmation dialog, then remove the selected row from the
list. When this happens, though, I will need to renumber all the
other div id’s in my list to keep the list in good, numerical order.
No, you really really don’t want to do this. Sortables use the actual
ID of the related object to update the position column in the database
for the appropriate records.
If you’re using the sortable list helpers, you must have something
like this in your view:
And when you sort, the callback sends an array of these IDs back to
the server in their current order.
You do want to have a separate callback to remove the object from the
collection you’re sorting, but that’s outside the scope of your
Each time you update the position attribute in the database, you need
to be doing that against a fresh lookup of your collection in your
controller (so you won’t try to sort a missing element). And when
you do, these IDs that are sent from the page must be the actual IDs
of the elements, not some arbitrary numerical order values, or else
you can’t update the correct elements with their new position value.
- def sort
@categories = Category.all
@categories.each do |category|
category.position = params[‘sort_list’].index(category.id.to_s)
render :nothing => true