# Sortable list not saving to database

**URL:** <https://www.ruby-forum.com/t/sortable-list-not-saving-to-database/93706>\
**Category:** Rails\
**Created:** [April 6, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/sortable-list-not-saving-to-database/93706 "2006-04-06T23:38:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 6, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/sortable-list-not-saving-to-database/93706/1 "2006-04-06T23:38:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 7, 2006, 1:07am UTC](https://www.ruby-forum.com/t/sortable-list-not-saving-to-database/93706/2 "2006-04-07T01:07:45Z")

</div>

> :url =\> { :action =\> ‘sort’, :id =\> @usr\_list },

@usr\_list might be your problem…

-Adam

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 7, 2006, 2:33pm UTC](https://www.ruby-forum.com/t/sortable-list-not-saving-to-database/93706/3 "2006-04-07T14:33:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [April 7, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/sortable-list-not-saving-to-database/93706/4 "2006-04-07T14:55:21Z")

</div>

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?
