nested_form icon indicating copy to clipboard operation
nested_form copied to clipboard

Newly generated ID should not be random

Open dylandrop opened this issue 12 years ago • 4 comments

I've been wondering what the reasoning was to make the newId function return Date().getTime(), which is essentially random, as opposed to something useful.

newId: function() {
  return new Date().getTime();
},

This is problematic for me -- I'm trying to add inline error messages for my nested resources. It would be much easier to do this if their ID used the count of their sibling fields to generate a new ID rather than something random.

For example, project[tasks_attributes][0], project[tasks_attributes][1] would be preferable to something like project[tasks_attributes][1255929127459], project[tasks_attributes][1255937127459]. These random IDs seem completely useless.

Lastly I think these would be easy to implement, as you can get .fields, and then count the nearest siblings to generate the next nested form's ID. What does everyone think?

dylandrop avatar Jun 21 '13 14:06 dylandrop

I believe the Date.getTime() id is used to let Rails know to insert a record instead of update. I'm new to all this so I may be wrong.

drewhamlett avatar Jun 28 '13 20:06 drewhamlett

Rails doesn't use the dom_id when building the nested objects, the dom_id is just presented in the view... That can't be right.

Also, if what you were saying was true, it would just look for that nested object with the ID of 1255937127459 (or whatever the time in seconds is)

dylandrop avatar Jun 28 '13 20:06 dylandrop

I solve it using: newId: function() { return $('div.fields').length; },

renatosousafilho avatar Aug 02 '13 17:08 renatosousafilho

I'm not sure if this $('div.fields').length idea would work if there are multiple fields_for as jQuery will count ALL the divs with class of .fields and not just the ones within that fields_for loop.

TheDevster avatar Dec 17 '13 22:12 TheDevster