I’m working on a form with JavaScript which generates hidden inputs for a nested model. It looks somewhat like this:
<!-- Attrs. inputs for nested model price -->
<button>Add price</button>
The jscript pop outs a bootstrap modal with fields to passing the attributes of a new price and generates hidden inputs for them, which look like this:
<input type="hidden" value="1" name="product[prices_attributes][0][quantity]">
<input type="hidden" value="123" name="product[prices_attributes][0][amount]">
which means, for each new price I want to send to the controller, I must generate ids. The question is, is there any way to make the controller generate the ids dynamically, instead of setting those myself?
One way is to get the current timestamp instead of the
[0]you’re using. Indeed this won’t be used as the ID of the new object. The ID will be created when the record will be saved in the DB. Those ‘ids’ you’re using are only used to identify different objects, so you only need for them to be unique, so the timestamp is a good solution for that.I would also suggest you try this gem : https://github.com/ryanb/nested_form