Skip to content

Instantly share code, notes, and snippets.

@bqst
Forked from brunopgalvao/_form.html.erb
Created June 11, 2019 12:18
Show Gist options
  • Save bqst/323cc5b91abc29f6926796b1355f6b4c to your computer and use it in GitHub Desktop.
Save bqst/323cc5b91abc29f6926796b1355f6b4c to your computer and use it in GitHub Desktop.
HTML5 Datalist using Ruby on Rails form_for
<%= form_for @person do |f| %>
<%= f.label :first_name, "First Name" %>:
<%= f.text_field :first_name, list: 'first-name' %>
<datalist id="first-name">
<% Person.all.each do |person| %>
<option value="<%= person.first_name %>"></option>
<% end %>
</datalist>
<%= f.submit %>
<% end %>
You may also want to do distinct:
<% Person.select(:first_name).distinct.each do |person| %>
@bqst
Copy link
Author

bqst commented Jun 11, 2019

datalist#first-name
= options_for_select(Person.pluck(:first_name).uniq, @person.first_name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment