Forum: Ruby on Rails sortable list not saving to database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
66d1d0df01e4bbb82674a353ea68d6ee?d=identicon&s=25 Josh Kieschnick (jjkiesch)
on 2006-04-06 23:38
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:

<h2><%= @user_list.full_name %>'s Tasks</h2>
<ul id="user-list">
	<% @user_list.tasks.each do |task| -%>
	<li id="item_<%= task.id %>">
		<%= task.task_name %>
	</li>
	<% end %>
</ul>
<%= 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?
6ec1e89eef5ce09fbfa56be5b6dd17b5?d=identicon&s=25 Adam Bloom (admanb)
on 2006-04-07 01:07
> 	:url => { :action => 'sort', :id => @usr_list },

@usr_list might be your problem...

-Adam
66d1d0df01e4bbb82674a353ea68d6ee?d=identicon&s=25 Josh Kieschnick (jjkiesch)
on 2006-04-07 14:33
yeah, saw that and almost slapped myself for letting something like that
get by... unfortunately it didn't fix anything.
66d1d0df01e4bbb82674a353ea68d6ee?d=identicon&s=25 Josh Kieschnick (jjkiesch)
on 2006-04-07 14:55
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?
This topic is locked and can not be replied to.