1

我正在通過jQuery UI構建一個拖放列表,用戶可以拖動項目列表(類別),並儘快當一個項目被拖動時,整個事件被序列化並傳遞給控制器​​中的'sort'函數作爲參數/ params [:category]來索引每個項目並更新它的位置。爲了簡化這個儘可能多的嘗試和追捕下面的錯誤,我實際上手動設置字符串在JavaScript中的現在。 (我的javascript,視圖和控制器代碼在下面這篇文章的末尾)。Ruby on Rails - 在添加太多項目時拋出jQuery拖放排序順序

這是問題所在。任何東西最多五件,它的作品美麗。控制檯輸出...

Started POST "/categories/sort" for 127.0.0.1 at Wed May 11 12:03:04 -0500 2011 
    Processing by CategoriesController#sort as 
    Parameters: {"category"=>{"1"=>{"id"=>"1"}, "2"=>{"id"=>"2"}, "3"=>{"id"=>"3"}, "4"=>{"id"=>"4"}, "5"=>{"id"=>"5"}}} 
    Category Load (0.3ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 1 LIMIT 1 
    AREL (0.5ms) UPDATE "categories" SET "position" = 1, "updated_at" = '2011-05-11 17:03:04.412058' WHERE "categories"."id" = 1 
    Category Load (0.3ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2 LIMIT 1 
    AREL (0.4ms) UPDATE "categories" SET "position" = 2, "updated_at" = '2011-05-11 17:03:04.416833' WHERE "categories"."id" = 2 
[...] 

然而對於一些絕對瘋狂的原因,在第六項/類別添加到字符串,突然,結果成了這樣...

Started POST "/categories/sort" for 127.0.0.1 at Wed May 11 12:12:35 -0500 2011 
    Processing by CategoriesController#sort as 
    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".* FROM "categories" WHERE "categories"."id" = 6 LIMIT 1 
    AREL (0.4ms) UPDATE "categories" SET "position" = 1, "updated_at" = '2011-05-11 17:12:35.358963' WHERE "categories"."id" = 6 
    Category Load (0.2ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 1 LIMIT 1 
    AREL (0.2ms) UPDATE "categories" SET "position" = 2, "updated_at" = '2011-05-11 17:12:35.362330' WHERE "categories"."id" = 1 
    Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = 2 LIMIT 1 
[...] 

正如你所看到的,當添加類別編號6時,該類別的參數用完,併成爲列表中的第一個項目,循環遍歷索引,完全拋棄正確的順序。更令人感到瘋狂的是,如果我在參數字符串中保留'category [6] [id] = 6'部分並在其之前排除少數部分,那麼無論如何,第6部分總是第一部分。

這使得絕對沒有意義,唯一我能想到的是,我沒有正確格式化javascript中的參數字符串。我究竟做錯了什麼?


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 do |id, index| 
    category_id = id[1][:id] 
    Category.update(category_id, :position => index + 1) 
    end 
    render :nothing => true 
end 

回答

0

我能終於流下爲什麼這是通過這個線程發生一些輕:My hashes are stacking unordered..What's a loop that can select them by their Hash ID?

我正在REE 1.8.7,爲此我的參數是不被排序。我能夠用替換這個我排序控制器功能的each_with_index行來解決這個問題...

params[:category].sort { |a, b| a <=> b }.each_with_index do |id, index| 

】這個params是在函數內部使出現在更新以正確的順序數據庫。

+0

另外,在我的代碼的實際版本中,我試圖通過排序的數字列是模式中的字符串而不是整數,這也沒有幫助。 – Shannon 2011-05-11 20:09:39