rails/actionview/CHANGELOG.md
Sean Doyle b8c9c9d06f Translate FormBuilder#button calls with formmethod:
When submitting a `<form>`, browsers will serialize the element that
initiated the submission as part of the [FormData][], including its
`name` and `value` attributes.

Browser support for `<form>` submission HTTP verbs is limited to `GET`
and `POST`. Rails currently works around this [limitation by
constructing `<input type="hidden" name="_method" value="VERB">` which
serializes `_method="VERB"` to the FormData][_method].

To support varied HTTP actions within the same form, this commit
intervenes when a `form.button formmethod: "..."` call is made during
form construction, and translates any `formmethod:` value to the
corresponding work-around version.

[FormData]: https://developer.mozilla.org/en-US/docs/Web/API/FormData
[_method]: https://edgeguides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark
[button-formmethod]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formmethod
2021-01-08 18:21:02 -05:00

3.1 KiB

  • Change ActionView::Helpers::FormBuilder#button to transform formmethod attributes into _method="$VERB" Form Data to enable varied same-form actions:

    <%= form_with model: post, method: :put do %>
      <%= form.button "Update" %>
      <%= form.button "Delete", formmethod: :delete %>
    <% end %>
    <%# => <form action="posts/1">
        =>   <input type="hidden" name="_method" value="put">
        =>   <button type="submit">Update</button>
        =>   <button type="submit" formmethod="post" name="_method" value="delete">Delete</button>
        => </form>
    %>
    

    Sean Doyle

  • Change ActionView::Helpers::UrlHelper#button_to to always render a <button> element, regardless of whether or not the content is passed as the first argument or as a block.

    <%= button_to "Delete", post_path(@post), method: :delete %>
    <%# => <form action="/posts/1"><input type="hidden" name="_method" value="delete"><button type="submit">Delete</button></form>
    
    <%= button_to post_path(@post), method: :delete do %>
      Delete
    <% end %>
    <%# => <form action="/posts/1"><input type="hidden" name="_method" value="delete"><button type="submit">Delete</button></form>
    

    Sean Doyle, Dusan Orlovic

  • Add config.action_view.preload_links_header to allow disabling of the Link header being added by default when using stylesheet_link_tag and javascript_include_tag.

    Andrew White

  • The translate helper now resolves default values when a nil key is specified, instead of always returning nil.

    Jonathan Hefner

  • Add config.action_view.image_loading to configure the default value of the image_tag :loading option.

    By setting config.action_view.image_loading = "lazy", an application can opt in to lazy loading images sitewide, without changing view code.

    Jonathan Hefner

  • ActionView::Helpers::FormBuilder#id returns the value of the <form> element's id attribute. With a method argument, returns the id attribute for a form field with that name.

    <%= form_for @post do |f| %>
      <%# ... %>
    
      <% content_for :sticky_footer do %>
        <%= form.button(form: f.id) %>
      <% end %>
    <% end %>
    

    Sean Doyle

  • ActionView::Helpers::FormBuilder#field_id returns 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) } %>
      <%= tag.span("is blank", id: f.field_id(:title, :error) %>
    <% end %>
    

    Sean Doyle

  • Add tag.attributes to transform a Hash into HTML Attributes, ready to be interpolated into ERB.

    <input <%= tag.attributes(type: :text, aria: { label: "Search" }) %> >
    # => <input type="text" aria-label="Search">
    

    Sean Doyle

Please check 6-1-stable for previous changes.