Repository: rails/rails

Issue #26931: Array of nested fields_for bug
Created: 2016-10-29T18:13:58Z
I am trying to construct a form which should be structured like this (simplified for clarity):
{
  users: [
    {
      username: String,
      contacts: {
        full_name: String
      },
      password: String
    },
    ...
  ]
}
That is, users is an array of objects with nested attributes.
Steps to reproduce
In a view:
<%= fields_for 'users[][]', @user do |user_fields| %>
  <%= user_fields.text_field :username %>
  <% puts user_fields.object_name # users[][] %>
  <%= user_fields.fields_for :contacts, OpenStruct.new(@user.contacts) do |contact_fields| %>
    <%= contact_fields.text_field :full_name %>
  <% end %>
  <% puts user_fields.object_name # users[] %>
  <%= user_fields.password_field :password %>
<% end %>
@user.contacts is a hash, which contains contact information for the user.
As you can see, after the fields_for :contacts, the name of the user_fields builder is stripped of one set of []. This interferes with the rest of the form.
Expected behavior
The above should approximately render as:
<input type="text" name="user[][username]" />
<input type="text" value="" name="user[][contacts][full_name]" />
<input type="password" name="user[][password]" />
Actual behavior
<input type="text" name="user[][username]" />
<input type="text" value="" name="user[][contacts][full_name]" />
<input type="password" name="user[password]" />
System configuration
Rails version: 5.0.0.1
Ruby version: 2.3.0

Duplicate issue #26942: Batch updating associations using `fields_for` and array names requires extra array
Created: 2016-10-31T18:27:31Z
Steps to reproduce
Application upgraded from 4.2 to 5.0.0 following the official upgrade guides.
Unusual behaviour in fields_for when used to batch create or update records using model[].
(Guidelines for creating a bug report are available
here)
Expected behavior
Something like the following should create an input to update an attribute of an association that belongs_to a record, customisable:
= form_for(controller: 'records', action: 'batch_create') do
  - @records.each do |record|
    = fields_for('associations[]', record.association) do |assoc|
      = assoc.number_field(:association_attribute)
Expected HTML:
<form foo="bar">
  <input type="number" name="associations[1][association_attribute]" />
  <input type="number" name="associations[2][association_attribute]" />
  <!-- repeat for each associated record -->
</form>
Actual behavior
HTML produced:
<form foo="bar">
  <input type="number" name="associations[association_attribute]" />
  <input type="number" name="associations[association_attribute]" />
  <!-- repeat for each associated record -->
</form>
This produces the same behaviour as if we had just used fields_for('associations')
System configuration
Rails version: 5.0.0
Ruby version: 2.2.3

Closing comment (2016-11-08T23:48:01Z): Can you create an executable test script using one of the templates here, that would demonstrate the problem you are seeing? Also, since you're saying this worked for you in Rails 4.2.x, can you do a git bisect to determine where a possible regression may have been introduced?
On a very quick look, looks like this could be related to #26931.