Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use nested_form.object in nested_fields_for_builder #62

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

efatsi
Copy link

@efatsi efatsi commented Sep 20, 2022

I was attempting to render some nested fields for a specific child instance, and caught an edge case in nested_fields_for_builder.

My first attempt:

<%# Find the administrate nested_has_many attribute for rounds %>
<% round_field = page.attributes.detect{|a| a.attribute == :rounds} %>

<%# This code yanked from nested_has_many _form & _field partials %>
<%= f.fields_for :rounds, @round do |nested_form| %>
  <div class="nested-fields">
    <% round_field.nested_fields_for_builder(nested_form).each do |attribute| -%>
      <div class="field-unit field-unit--<%= attribute.html_class %>">
        <%= render_field attribute, f: nested_form %>
      </div>
    <% end -%>
  </div>
<% end %>

By passing in a specific round instance to f.fields_for :rounds, @round ..., I would expect the form fields to render with the correct round instance. However, the resource is found like so in nested_fields_for_builder

resource = data[form_builder.index]

If you pass an instance to fields_for, the form_builder's instance will always be 0, even though you could be operating on a different child.

I propose to lean on the form_builder.object instead, which I believe should always be correctly set to the resource at hand.

@nickcharlton
Copy link
Owner

Is there anything we could do for testing here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants