Frage

I have a select_tag with options_for_select in a form_for to create an instance of a model but it's a huge list so I want to add a search (allow the user to type their selection and see the narrowed down results as they keep typing) instead of a select. Should I replace the select with a form_tag? How would I go about adding a search field within the form_for?

views/dogs/new:

<%= form_for [@master, @dog], role: "form" do |f| %>

              <div class="form-group">
                  <div><%= f.label :name %><br />
                  <%= f.text_field :name, :autofocus => true, class: "form-control" %></div>
              </div>

              <div class="form-group">
                  <div><%= f.label :age %><br />
                  <%= f.number_field :age, class: "form-control" %></div>
              </div>

              <div class="form-group">
                  <div><%= f.label :breed %><br />
                  <%= select_tag "breed", options_for_select([['French Bulldog' ,'French Bulldog'], ['Pitbull', 'Pitbull']]) %>

              </div>

currently appears as:

enter image description here

War es hilfreich?

Lösung

You would need to use a text field instead of a select field and use jQuery autocomplete(http://jqueryui.com/autocomplete/).

There's a nice example on how to use it.

You would need just to populate a variable in js with all the breeds in it and then use the plugin to filter the list and use one of its callback in case you need to customize your text field further; e.g. after a user has chosen the breed.

Hope this helps you

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top