-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
ActionView::FormBuilder#field_name returns incorrect field name for deeply-nested associations #45483
Comments
Tagging @seanpdoyle bc it looks like he introduced |
Thank you for opening this issue @shanecav84. I've reproduced it with a failing test on a branch on my fork: https://github.com/rails/rails/compare/seanpdoyle:action-view-nested-field-name-calls. I'll investigate! |
@seanpdoyle @shanecav84 : Fixed this issue here: #45523 |
That PR fixes the issue I am having. |
I've also encountered this in the wild while attempting to upgrade an app to Rails 7. i.e. # In _form.html.haml
= item.products.each do |product|
= f.fields_for :product, product do |pf|
#product-description-text-area { data: { id: pf.field_id(:description), name: pf.field_name(:description) } }
# where the form object's model is like
class Item < ApplicationRecord
has_many :products
accepts_nested_attributes_for :products
end
# Expected result in `data-id` attribute
# => item_products_attributes_0_description
# Actual
# => item_products_attributes_0_0_description The use case here is to pass rails style form element attributes to the javascript layer to so that it can build a fancier form element. For the moment, I have been able to workaround for this specific use case by explicitly setting the index to nil, i.e. |
Closes rails#45483 Closes rails#45523 To quote rails#45483: > `field_name` is adding an extra index parameter, the extra `[0]` in > `parent[children_attributes][0][0][grandchildren_attributes][]` To resolve that issue, this commit reads its default `field_id` and `field_name` method's `index:` option directly from the `@options`. Prior to this commit, that value was read from the `@index` instance variable.
Closes #45483 Closes #45523 To quote #45483: > `field_name` is adding an extra index parameter, the extra `[0]` in > `parent[children_attributes][0][0][grandchildren_attributes][]` To resolve that issue, this commit reads its default `field_id` and `field_name` method's `index:` option directly from the `@options`. Prior to this commit, that value was read from the `@index` instance variable. Signed-off-by: Akira Matsuda <ronnie@dio.jp>
field_name
is adding an extra index parameter, the extra[0]
inparent[children_attributes][0][0][grandchildren_attributes][]
Steps to reproduce
Expected behavior
Don't add an extra index parameter (parent[children_attributes][0]
[0][grandchildren_attributes][])Actual behavior
An extra index parameter is added
System configuration
Rails version: 7
Ruby version: 3
The text was updated successfully, but these errors were encountered: