nathanvda / cocoon

Dynamic nested forms using jQuery made easy; works with formtastic, simple_form or default forms
http://github.com/nathanvda/cocoon
MIT License
3.08k stars 383 forks source link

form_with resulting in undefined local variable in link_to_add_association #586

Closed jberry closed 4 years ago

jberry commented 4 years ago

I am attempting to implement cocoon inside of a form_with form, and running into:

"undefined local variable or method `form' for <Class:0x000000000430b8e0>:0x00000000040ea250>

Showing app/views/job_step_options/_job_step_option_fields.erb where line #3 raised:

My form definition starts with: <%= form_with(model: job_definition_step, local: true) do |form| %>

And I am attempting to setup the link_to as such: <%= link_to_add_association 'add option', form, :job_step_options, partial:'job_step_options/job_step_option_fields' %>

And my partial is very basic right now (which appears to be where the error is thrown, line 3 in this file which is calling form.label):

<div class="nested-fields" >
  <div class="row">
    <div class="col-sm-1"><%= form.label :option_type %></div>
  </div>
  <%= link_to_remove_association "remove option", form %>
</div>

In my debug console poking at the form variable:

>> form.present?
=> true
>> form.class
=> ActionView::Helpers::FormBuilder
>> form.form
NoMethodError: undefined method `form' for #<ActionView::Helpers::FormBuilder:0x00007f2734e83ea8>

This is running with cocoon 1.2.14, ontop ruby 2.5.3 and rails 6.0.2.1.

I'm not sure what all to attach for additional data, but glad to provide whatever is necessary/dumps of variables/etc as requested.

jberry commented 4 years ago

Tracked it down with other google searches that lead to other Cocoon errors; End answer: specify form_name: 'form' in the link_to_add; Hopefully my including "form_with" in the issue name will help lead others here as well :)

Broke:

<%= link_to_add_association 'add option', form, :job_step_options, partial: 'job_step_options/job_step_option_fields' %>

Working with additional form_name field:

<%= link_to_add_association 'add option', form, :job_step_options, partial: 'job_step_options/job_step_option_fields', form_name: 'form' %>

https://github.com/nathanvda/cocoon/issues/504