Ruby on Rails - jQuery drag and drop sort order thrown off when too many items are added -
i'm building drag-and-drop list via jquery ui users can drag list of items (categories), , item dragged, whole thing serialized , passed 'sort' function in controller parameters/params[:category] index through each item , update it's position. sake of simplifying as possible try , hunt following bug out, manually setting string in javascript now. (my javascript, view , controller code @ end of post below).
here's problem. 5 items, works beautifully. console outputs...
started post "/categories/sort" 127.0.0.1 @ wed may 11 12:03:04 -0500 2011 processing categoriescontroller#sort parameters: {"category"=>{"1"=>{"id"=>"1"}, "2"=>{"id"=>"2"}, "3"=>{"id"=>"3"}, "4"=>{"id"=>"4"}, "5"=>{"id"=>"5"}}} category load (0.3ms) select "categories".* "categories" "categories"."id" = 1 limit 1 arel (0.5ms) update "categories" set "position" = 1, "updated_at" = '2011-05-11 17:03:04.412058' "categories"."id" = 1 category load (0.3ms) select "categories".* "categories" "categories"."id" = 2 limit 1 arel (0.4ms) update "categories" set "position" = 2, "updated_at" = '2011-05-11 17:03:04.416833' "categories"."id" = 2 [...]
however absolutely crazy reason, add in sixth item/category string, , results become this...
started post "/categories/sort" 127.0.0.1 @ wed may 11 12:12:35 -0500 2011 processing categoriescontroller#sort parameters: {"category"=>{"6"=>{"id"=>"6"}, "1"=>{"id"=>"1"}, "2"=>{"id"=>"2"}, "3"=>{"id"=>"3"}, "4"=>{"id"=>"4"}, "5"=>{"id"=>"5"}}} category load (0.2ms) select "categories".* "categories" "categories"."id" = 6 limit 1 arel (0.4ms) update "categories" set "position" = 1, "updated_at" = '2011-05-11 17:12:35.358963' "categories"."id" = 6 category load (0.2ms) select "categories".* "categories" "categories"."id" = 1 limit 1 arel (0.2ms) update "categories" set "position" = 2, "updated_at" = '2011-05-11 17:12:35.362330' "categories"."id" = 1 category load (0.1ms) select "categories".* "categories" "categories"."id" = 2 limit 1 [...]
as can see, when adding category number six, params category runs out , becomes first item in list loop through index with, throwing off correct order. whats far more crazy if leave 'category[6][id]=6' portion in parameters string , eliminate few before it, number 6 first no matter what.
this makes absolutely no sense, , thing can figure i'm not formatting parameters string in javascript correctly. doing wrong?
index.html.erb
<ul id="categories"> <li id="category_1"><div class="handle"></div>one</li> <li id="category_2"><div class="handle"></div>two</li> <li id="category_3"><div class="handle"></div>three</li> <li id="category_3"><div class="handle"></div>four</li> <li id="category_3"><div class="handle"></div>five</li> <li id="category_3"><div class="handle"></div>six</li> </ul>
application.js
$("#categories").sortable({ opacity: 0.6, handle: '.handle', update: function(event, ui) { var parameters = 'category[1][id]=1&category[2][id]=2&category[3][id]=3&category[4][id]=4&category[5][id]=5&category[6][id]=6'; $.post("/categories/sort", parameters); } });
categories_controller.rb
def sort params[:category].each_with_index |id, index| category_id = id[1][:id] category.update(category_id, :position => index + 1) end render :nothing => true end
i able shed light on why happening via thread: my hashes stacking unordered..what's loop can select them hash id?
i running ree 1.8.7, therefor param not being sorted. able solve issue replacing each_with_index line in sort controller function this...
params[:category].sort { |a, b| <=> b }.each_with_index |id, index|
the params resorted inside function , update database in correct order.
Comments
Post a Comment