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

Introduce field_name view helper #43409

Merged

Conversation

seanpdoyle
Copy link
Contributor

The field_name helper and corresponding FormBuilder#field_name
method provide an Action View-compliant way of overriding a form field
element's [name] attribute (similar to field_id and
FormBuilder#field_id introduced in #40127[][]).

text_field_tag :post, :title, name: field_name(:post, :title, :subtitle)
  # => <input type="text" name="post[title][subtitle]">

text_field_tag :post, :tag, name: field_name(:post, :tag, multiple: true)
  # => <input type="text" name="post[tag][]">

form_for @post do |f|
  f.field_tag :tag, name: f.field_name(:tag, multiple: true)
  # => <input type="text" name="post[tag][]">
end

Copy link
Member

@p8 p8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I think this is really useful to extract as a helper.

The `field_name` helper and corresponding `FormBuilder#field_name`
method provide an Action View-compliant way of overriding a form field
element's `[name]` attribute (similar to `field_id` and
`FormBuilder#field_id` introduced in rails#40127[][]).

```ruby
text_field_tag :post, :title, name: field_name(:post, :title, :subtitle)
  # => <input type="text" name="post[title][subtitle]">

text_field_tag :post, :tag, name: field_name(:post, :tag, multiple: true)
  # => <input type="text" name="post[tag][]">

form_for @post do |f|
  f.field_tag :tag, name: f.field_name(:tag, multiple: true)
  # => <input type="text" name="post[tag][]">
end
```

[rails#40127]: rails#40127
@seanpdoyle seanpdoyle force-pushed the action-view-form-builder-field-name branch from 3a87121 to 37081bf Compare October 29, 2021 14:00
@rafaelfranca rafaelfranca merged commit 876d2ff into rails:main Nov 15, 2021
@seanpdoyle seanpdoyle deleted the action-view-form-builder-field-name branch November 15, 2021 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants