Search code examples
ruby-on-railsnested-formsranking

rank contact's phone numbers in rails


I'm trying to order or rank a contacts phone numbers in rails so that users can change the order of a phone number. I'm using the nested form gem if that helps.

The idea is that I want to rank them based the order in which they come in. so the user could move the phone number field up or down the list and then based on what position in the list the number is in, it would be ranked in that order.

so like it would be listed as phone1 phone2 phone3 phone4

then the user could change the order to phone2 phone4 phone1 phone3

and rails would know to re order them, ie, change phone2's rank to phone1 phone 4's rank to phone2 etc.

I could change the order on the front end, but what that feels like there could be issues thanks in advance


Solution

  • I'd run with something like https://github.com/swanandp/acts_as_list to manage the ranking stuff, but as far as the front-end goes, your best bet is to either use hidden form fields for the ordering (and update their values via JS), or each time a number is moved (via some sort of javascript goodness), submit that to a controller via xhr and re-render the list of numbers as a partial.