In my app, when I select items from a list using check boxes, their IDs are passed to the controller in ascending numerical order, regardless of the original sequence of selection. I've noticed that the params organize the IDs this way automatically. This results in the selected items being stored and shown without respecting the order they were originally selected. How can I keep the original order of selection? Thank you.
Asked
Active
Viewed 183 times
0
-
Hashes do not care about order so they disregard the order you "wanted". How about you send those IDs as an array. Arrays do not disregard the order and hence you will be easy off. What is your use-case. I suggest going with array approach. I hope someone has direct solution to your question. – ARK Jan 27 '20 at 07:41
-
1@ARK thats not true anymore. "Hashes enumerate their values in the order that the corresponding keys were inserted." See https://ruby-doc.org/core-2.5.1/Hash.html and https://stackoverflow.com/questions/31418673/is-order-of-a-ruby-hash-literal-guaranteed – max Jan 27 '20 at 11:06
-
This has nothing to do with how ruby treats hashes. Its all up to how Rack parses FormData into arrays and hashes. – max Jan 27 '20 at 11:09
-
@max thanks. That was knowledge for me :) But don't you get what I am trying to say? I mean if that is happening to params (ordering ascending), OP could use array of IDs in params and avoid the issue? – ARK Jan 27 '20 at 11:13