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

Array of nested fields_for bug #26931

Closed
ollpu opened this issue Oct 29, 2016 · 5 comments
Closed

Array of nested fields_for bug #26931

ollpu opened this issue Oct 29, 2016 · 5 comments

Comments

@ollpu
Copy link

ollpu commented Oct 29, 2016

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

@ollpu ollpu changed the title Array of nested fields_for fails Array of nested fields_for bug Oct 29, 2016
@ollpu
Copy link
Author

ollpu commented Oct 30, 2016

I dug around the source, and the culprit is clearly here. Is there proper way to make an array of objects without having to name each field manually?

@vedant1811
Copy link

I am able to reproduce this as well.

@maclover7
Copy link
Contributor

Can you take your code from above and create an executable test script using one of the templates here, that would demonstrate the problem you are seeing? I'm trying to reproduce the issue locally, and am having some trouble doing so.

@erickueen
Copy link

I was able to reproduce this too, but it does not seem to be a bug, as you pointed, the problem is that the object name is overwrote when an auto index is defined.
I found that if you define an index when calling the first fields_for, rails reproduce the expected behavior.

So if you have an array of users, you can do something like this:

<%= fields_for 'users', @user {index: @user.id} do |user_fields| %>

So the user ID is used as the index, and the object name will not be overwrote.

@ollpu
Copy link
Author

ollpu commented Nov 16, 2016

@erickueen this would be fine otherwise, but I'm also adding new users to the form dynamically via javascript. This would mean that I couldn't just copy a template to the form, but have to change all the field names as well.
Also, since I'm creating new users, and hence they don't have any id yet, unpacking the hash with completely unrelated id:s causes redundant code.

y-yagi added a commit to y-yagi/rails that referenced this issue Dec 27, 2016
Since it affects the entire form, should not mutate `object_name` of class.

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

No branches or pull requests

4 participants