Sortable list not saving to database


#1

i am trying to use a sortable list on my app. i used the chapter in
Rails recipes as a reference on how to do it. i have the following code
in my controllerr:

def sort
@user_list = User.find(params[:id])
@user_list.tasks.each do |task|
task.position = params[‘user-list’].index(task.id) + 1
task.save
end
render :nothing => true
end

and in my view, i have:

<%= @user_list.full_name %>'s Tasks

    <% @user_list.tasks.each do |task| -%>
  • <%= task.task_name %>
  • <% end %>
<%= sortable_element 'user-list', :url => { :action => 'sort', :id => @usr_list }, :complete => visual_effect(:highlight, 'user-list') %>

everything seems like it works great… until i refresh the screen and
see that everything is back to the way it was. i look in the database,
and sure enough… nothing has changed. did i do something wrong?


#2

:url => { :action => ‘sort’, :id => @usr_list },

@usr_list might be your problem…

-Adam


#3

yeah, saw that and almost slapped myself for letting something like that
get by… unfortunately it didn’t fix anything.


#4

i figured it out. there were some other things i had changed while
trying to get it to work after the first error. changed everything back
and it is fine now.

it does bring up some questions i have about the sortable lists though.
i can make one now, but i would like to have a better understanding of
how everything works.

one line in particular:
task.position = params[‘user-list’].index(task.id.to_s) + 1

how does this line get the right value? what is the value of
index(task.id.to_s) and why is it to_s instead of just task.id? wouldn’t
it need to be numerical in order to have 1 added to it?