-
Notifications
You must be signed in to change notification settings - Fork 506
Duplicate fields on 'render' for already saved fields (ex: form validation failed) #346
Comments
Woh this is so weird. The objects in memory are completely messed up. Let's say I have a project with a task saved in the database. Now if I render the task invalid via a In the controller,
|
Found the source of my problem. Eager Loading In my controller, I was actually eager-loading before trying to update the values
|
Hi there guys, I'm having exactly the same problem My controller looks like this tho.. And it seems that somewhere im double building my parent instance but I can't see where. Any hints? My controller looks like this
|
And here is the model
|
And this is the view
|
You can make this work by only having one simple_fields_for in your view, and building your doses twice. So in your new action something like 2.times { cocktail.doses.build } This SO answer helped me figure this out as I was having the same issue The idea from what I understand is how form_builder goes about generating fields for associated models. In this case, when you initially build you only associated one dose to your model, and the form builder generated two inputs for the same model. When a validation failed, there were now two associated models (doses) from the two separate input fields on your cocktail model. Then when it went to render the form again, the first time it encounters simple fields for it will generate fields for BOTH doses associated to your model, and when it got to the second simple fields for, it did it again, totaling four doses on the page, and each time you'd try and submit it would continue this pattern of exponentially increasing! |
My nested_form works fine, EXCEPT when in my controller, I
render
(notredirect_to
) a page that contains a nested_form, with some nested_fields already saved in the database. It duplicates (only in the HTML) every nested field that has already been saved. So i end up with two hidden fields with the same IDModel
In my controller
Every nested_fields in my form are more or less like this one (in this case
echange(s)
is the name of the association)Before I submit, I have a single field with the invisible id input (that is already saved)
Now When I resubmit this single field, this time with validation errors, I end up with two
...And only validation error messages are shown on the second one
If I had 3 already saved nestd_field before rendernig validation errors, I would've ended up with 6 fields (3 duplicates of the original 3)
Did I miss something ?
Rails 4.2
Mongoid 4
Nested form 0.3.2
The text was updated successfully, but these errors were encountered: