

Declare ActionView::Helpers::FormBuilder#id and #field_id
source link: https://github.com/rails/rails/pull/40127
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Summary
ActionView::Helpers::FormBuilder#id
Generate an HTML id
attribute value.
Return the <form>
element's id
attribute.
<%= form_for @post do |f| %> <%# ... %> <% content_for :sticky_footer do %> <%= form.button(form: f.id) %> <% end %> <% end %>
In the example above, the :sticky_footer
content area will exist
outside of the <form>
element. By declaring the form
HTML
attribute, we hint to the browser that the
generated <button>
element should be treated as the <form>
element's
submit button, regardless of where it exists in the DOM.
A similar pattern could be used for <input>
elements (or other form controls) that do not
descend from the <form>
element.
ActionView::Helpers::FormBuilder#field_id
Generate an HTML id attribute value for the given field
Return the value generated by the FormBuilder for the given
attribute name.
<%= form_for @post do |f| %> <%= f.label :title %> <%= f.text_field :title, aria: { describedby: f.field_id(:title, :error) } %> <span id="<%= f.field_id(:title, :error) %>">is blank</span> <% end %>
In the example above, the element built by
the call to FormBuilder#text_field declares an
aria-describedby attribute referencing the
element, sharing a common id root (post_title, in this
case).
This method is powered by the field_id
helper declared inaction_view/helpers/form_tag_helper
, which is made available for
general template calls, separate from a FormBuilder
instance.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK