Skip to content

Use nested_form.object in nested_fields_for_builder#62

Open
efatsi wants to merge 1 commit into
nickcharlton:mainfrom
efatsi:patch-1
Open

Use nested_form.object in nested_fields_for_builder#62
efatsi wants to merge 1 commit into
nickcharlton:mainfrom
efatsi:patch-1

Conversation

@efatsi
Copy link
Copy Markdown

@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
Copy Markdown
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