{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/27031","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/27031/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/27031/comments","events_url":"https://api.github.com/repos/rails/rails/issues/27031/events","html_url":"https://github.com/rails/rails/issues/27031","id":188996909,"number":27031,"title":"fields_for used with collection drops data after failed validation","user":{"login":"adas172002","id":5249598,"avatar_url":"https://avatars.githubusercontent.com/u/5249598?v=3","gravatar_id":"","url":"https://api.github.com/users/adas172002","html_url":"https://github.com/adas172002","followers_url":"https://api.github.com/users/adas172002/followers","following_url":"https://api.github.com/users/adas172002/following{/other_user}","gists_url":"https://api.github.com/users/adas172002/gists{/gist_id}","starred_url":"https://api.github.com/users/adas172002/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adas172002/subscriptions","organizations_url":"https://api.github.com/users/adas172002/orgs","repos_url":"https://api.github.com/users/adas172002/repos","events_url":"https://api.github.com/users/adas172002/events{/privacy}","received_events_url":"https://api.github.com/users/adas172002/received_events","type":"User","site_admin":false},"labels":[{"id":3666649,"url":"https://api.github.com/repos/rails/rails/labels/actionview","name":"actionview","color":"d7e102","default":false},{"id":107191,"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1","default":false},{"id":257468118,"url":"https://api.github.com/repos/rails/rails/labels/needs%20work","name":"needs work","color":"000000","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":11,"created_at":"2016-11-13T21:39:16Z","updated_at":"2016-12-23T20:42:44Z","closed_at":null,"body":"### Steps to reproduce\r\n\r\nModel with several `has_many` relation has a form_for with nested `fields_for`, some of them are specified with a collection of children. More details described on [SO question](http://stackoverflow.com/q/40577263/3187415)\r\n\r\n### Expected behavior\r\nUpon failed validation, form should show all nested fields entered by user.\r\n\r\n### Actual behavior\r\nUpon failed validation, form is rendered with edit view, but nested children that have a collection specified in `fields_for` object do not show up on edit view, even if are still  present in model params hash. Only child objects that seem to be persisted are shown. \r\n\r\n### System configuration\r\n**Rails version**: 4.2.7.1\r\n\r\n**Ruby version**: 2.2.1"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/269043455","html_url":"https://github.com/rails/rails/issues/27031#issuecomment-269043455","issue_url":"https://api.github.com/repos/rails/rails/issues/27031","id":269043455,"user":{"login":"mluts","id":15202437,"avatar_url":"https://avatars.githubusercontent.com/u/15202437?v=3","gravatar_id":"","url":"https://api.github.com/users/mluts","html_url":"https://github.com/mluts","followers_url":"https://api.github.com/users/mluts/followers","following_url":"https://api.github.com/users/mluts/following{/other_user}","gists_url":"https://api.github.com/users/mluts/gists{/gist_id}","starred_url":"https://api.github.com/users/mluts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mluts/subscriptions","organizations_url":"https://api.github.com/users/mluts/orgs","repos_url":"https://api.github.com/users/mluts/repos","events_url":"https://api.github.com/users/mluts/events{/privacy}","received_events_url":"https://api.github.com/users/mluts/received_events","type":"User","site_admin":false},"created_at":"2016-12-23T20:42:44Z","updated_at":"2016-12-23T20:42:44Z","body":"@adas172002 your demo application is wrong\r\n\r\n```\r\n    <%= f.fields_for :components, @product.components.papers + @product.components.envelopes do |component| %>                          \r\n      <%= render partial: \"component_fields\", locals: {component: component} %>\r\n    <% end %>\r\n```\r\n\r\n`@product.components.papers` delegates \"papers\" to the `Component` class which results in empty result, because none of the `Component` records are persisted when the form is rendered with errors. Not persisted `components` in the `@product` are discarded.\r\n\r\nAfter applying this patch to your demo repository, it works as expected\r\n\r\n```diff\r\n app/views/products/_form.html.erb | 2 +-                                                                                                1 file changed, 1 insertion(+), 1 deletion(-)                                                                                          \r\n\r\ndiff --git a/app/views/products/_form.html.erb b/app/views/products/_form.html.erb\r\nindex a0b29da..5e81398 100644                                                                                                           --- a/app/views/products/_form.html.erb                                                                                                 +++ b/app/views/products/_form.html.erb\r\n@@ -20,7 +20,7 @@\r\n     <%= link_to_add_fields 'Add one more', f, :components, {}, 'ui orange tag label' %>\r\n   </h4>\r\n   <div class=\"ui attached segment\">\r\n-    <%= f.fields_for :components, @product.components.papers + @product.components.envelopes do |component| %>\r\n+    <%= f.fields_for :components, @product.components do |component| %>\r\n       <%= render partial: \"component_fields\", locals: {component: component} %>\r\n     <% end %>\r\n   </div>\r\n```\r\n\r\n![fields-for-error](https://cloud.githubusercontent.com/assets/15202437/21462130/13016280-c961-11e6-93d6-8d79c2923a8d.png)\r\n"}}
{"actor":{"display_login":"mluts"}}