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

Batch updating associations using fields_for and array names requires extra array #26942

Closed
sjmog opened this issue Oct 31, 2016 · 1 comment
Closed

Comments

@sjmog
Copy link

sjmog commented Oct 31, 2016

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

@maclover7
Copy link
Contributor

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.

rafaelfranca added a commit that referenced this issue Jan 3, 2017
When you ask to generate multiple nested inputs using:

    field_for('comments[]', Comment.new) do |c|
      c.text_field :body

Rails should generated the names like `post[comments][][body]`.

To make sure we don't have regression the fake models now use the same
implementation of `#to_param` as `ActiveRecord::Base`

Fixes #26942
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

2 participants