i have parent model, , child model. i'm trying make form creating parent , associated children @ same time.
in form, have button "add" new child (a parent can have many children user defines). when button clicked, adds hidden field child form , sets name attribute default saved when form submitted. i've been doing adding hidden_field_tag
form on button click event:
hidden_field_tag "parent[children_attributes][][name]", "something"
which creates:
<input id="children_attributes__name" name="parent[children_attributes][][name]" type="hidden" value="something">
this seems work! concerned empty []
. i'm not sure or why works, discovered does. safe use?
when @ form fields created using fields_for
, see sets numeric index middle array/hash (whatever is), so:
<input id="children_attributes_0_name" name="parent[children_attributes][0][name]" type="text">
unfortunately, can't use fields_for
because have add hidden fields when "add" button clicked.
what's right approach here?
your second question "what's right approach here?"
i use ryan bate's nested form. sorts kind of stuff out you.
your first question "is safe use?"
i dont know. if ever have manually, make sure unique value in [].
the gem believe uses timestamp keep unique.
Comments
Post a Comment