Support datalist HTML element

In order to generate an HTML datalist element with Rails I needed the following code:

<%= content_tag(:datalist, nil, id: :payees_datalist, 'data-autocomplete-input-target' => :datalist) do %>
  <% @payees.each do |payee| %>
    <%=
      content_tag(
        :option,
        nil,
        value: payee.name,
        data: { id: payee.id },
        'data-autocomplete-input-target' => 'datalistOption'
      )
    %>
  <% end %>
<% end %>

It would be great to have a simple way to do that. Something like the following code:

datalist_tag(:payee_datalist, @payees.map { # ... }, optional_html_options)
4 Likes

I’ve created the PR #52137.

1 Like