{"action":"created","issue":{"url":"https://api.github.com/repos/nathanvda/cocoon/issues/489","repository_url":"https://api.github.com/repos/nathanvda/cocoon","labels_url":"https://api.github.com/repos/nathanvda/cocoon/issues/489/labels{/name}","comments_url":"https://api.github.com/repos/nathanvda/cocoon/issues/489/comments","events_url":"https://api.github.com/repos/nathanvda/cocoon/issues/489/events","html_url":"https://github.com/nathanvda/cocoon/issues/489","id":303792328,"number":489,"title":"link_to_add_association fields not working when used with default input fields","user":{"login":"RuneDrop","id":37210360,"avatar_url":"https://avatars1.githubusercontent.com/u/37210360?v=4","gravatar_id":"","url":"https://api.github.com/users/RuneDrop","html_url":"https://github.com/RuneDrop","followers_url":"https://api.github.com/users/RuneDrop/followers","following_url":"https://api.github.com/users/RuneDrop/following{/other_user}","gists_url":"https://api.github.com/users/RuneDrop/gists{/gist_id}","starred_url":"https://api.github.com/users/RuneDrop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RuneDrop/subscriptions","organizations_url":"https://api.github.com/users/RuneDrop/orgs","repos_url":"https://api.github.com/users/RuneDrop/repos","events_url":"https://api.github.com/users/RuneDrop/events{/privacy}","received_events_url":"https://api.github.com/users/RuneDrop/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-03-09T10:22:30Z","updated_at":"2018-03-09T11:13:35Z","closed_at":null,"author_association":"NONE","body":"I'm rather new to Rails and never used Cocoon before, but it seems to be pretty useful so far :)\r\n\r\nI hope that I just haven't overlooked something obvious and stupid here, but I'm having issues getting link_to_add_association fields to work in combination with default rendered fields (I hope it's clear what I mean, but you will probably see what I'm trying to do in the code).\r\n\r\nI have a Voting and an Option model: \r\n```\r\nclass Voting < ApplicationRecord\r\n  has_many :options, inverse_of: :voting\r\n  accepts_nested_attributes_for :options, reject_if: :all_blank\r\n\r\n  validates :content, presence: { message: \"Error: Please enter your Voting Question.\" }\r\n  validate :option_counter\r\n\r\n  def option_counter\r\n    return errors.add :vote, \"Error: at least two Options required.\" if options.size < 2\r\n  end\r\nend\r\n```\r\n```\r\nclass Option < ApplicationRecord\r\n  belongs_to :voting\r\nend\r\n```\r\n(including the validations to stay on the safe side)\r\n\r\nmy new.html.erb:\r\n```\r\n<%= form_with model: @voting, local: true do |f| %>\r\n  <p>\r\n    <%= f.label :voting %><br>\r\n    <%= f.text_area :content , rows: 4, cols: 35%>\r\n  </p>\r\n  <p>\r\n    <%= f.label :options %>\r\n    <%= f.fields_for :options do |option| %>\r\n      <%= render 'option_fields', f: option %>\r\n    <% end %>\r\n    <%= link_to_add_association \"add option\", f, :options %>\r\n  </p>\r\n  <p>\r\n    <%= f.submit %>\r\n  </p>\r\n<% end %>\r\n```\r\nand the _option_fields.html.erb:\r\n```\r\n<ul><%= f.text_field :vote %></ul>\r\n```\r\n\r\nso you can create a voting object together with its nested option objects. \r\nthe link_to_add_association function works well so far, records are going to be saved in the DB and everything is fine.\r\nBUT I want to offer the user 3 option fields as default and then he can choose to add more via \"add options\"-link.\r\nThe problem is, as soon as I add `3.times { @voting.options.build }` or even just `@voting.options.build` into the new action of my VotingsController, the dynamic fields from Cocoon won't save their values into the DB anymore. Only the values added into the default fields will be saved, the values of the dynamically added fields are ignored. And I can't see why, I already searched for similar issues but couldn't find any.\r\n\r\nI'm using Rails 5.1.5 and Cocoon 1.2.11"},"comment":{"url":"https://api.github.com/repos/nathanvda/cocoon/issues/comments/371784785","html_url":"https://github.com/nathanvda/cocoon/issues/489#issuecomment-371784785","issue_url":"https://api.github.com/repos/nathanvda/cocoon/issues/489","id":371784785,"user":{"login":"nathanvda","id":34460,"avatar_url":"https://avatars3.githubusercontent.com/u/34460?v=4","gravatar_id":"","url":"https://api.github.com/users/nathanvda","html_url":"https://github.com/nathanvda","followers_url":"https://api.github.com/users/nathanvda/followers","following_url":"https://api.github.com/users/nathanvda/following{/other_user}","gists_url":"https://api.github.com/users/nathanvda/gists{/gist_id}","starred_url":"https://api.github.com/users/nathanvda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nathanvda/subscriptions","organizations_url":"https://api.github.com/users/nathanvda/orgs","repos_url":"https://api.github.com/users/nathanvda/repos","events_url":"https://api.github.com/users/nathanvda/events{/privacy}","received_events_url":"https://api.github.com/users/nathanvda/received_events","type":"User","site_admin":false},"created_at":"2018-03-09T11:13:35Z","updated_at":"2018-03-09T11:13:35Z","author_association":"OWNER","body":"I cannot think why `@voting.options.build` would be saved and a dynamically added option would not.\r\nBecause that is what you are saying right: only the pre-built options will be saved, and the other will not? \r\n\r\nCan you show us the parameters that are posted to the controller (you can find that in the logfile) and your strong-parameters definition? If your controller-code does something extra, maybe it might be useful to show that too? "}}
{"actor":{"display_login":"nathanvda"}}