Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4616414
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:58:41+00:00 2026-05-22T01:58:41+00:00

I’m building a drag-and-drop list via jQuery UI where users can drag a list

  • 0

I’m building a drag-and-drop list via jQuery UI where users can drag a list of items (categories), and as soon as an item is dragged, the whole thing is serialized and passed to the ‘sort’ function in the controller as the parameters/params[:category] to index through each item and update it’s position. For sake of simplifying this as much as possible to try and hunt the following bug out, I am actually manually setting the string in the javascript for now. (My javascript, view and controller code is at the end of this post below).

Here’s the problem. With anything up to five items, it works beautifully. Console outputs…

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
[...]

However for some absolutely crazy reason, add in a sixth item/category to that string, and suddenly the results become this…

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
[...]

As you can see, when adding category number six, the params for that category runs out and becomes the first item in the list to loop through the index with, completely throwing off the correct order. Whats far more crazy is that if I leave the ‘category[6][id]=6’ portion in the parameters string and eliminate a few before it, number six will always be the first no matter what.

This makes absolutely no sense, and the only thing I can figure is that I’m not formatting the parameters string in the javascript correctly. What am I 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 do |id, index|
    category_id = id[1][:id]
    Category.update(category_id, :position => index + 1)
  end
  render :nothing => true
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:58:42+00:00Added an answer on May 22, 2026 at 1:58 am

    I was able to finally shed some light on why this was happening via this thread: My hashes are stacking unordered..What's a loop that can select them by their Hash ID?

    I am running REE 1.8.7, therefor my param is not being sorted. I was able to solve the issue with replacing the each_with_index line in my sort controller function with this…

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

    The params are resorted inside the function and now update the database in the correct order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.