How to add data attribute in Rails form select tag?

Zulhilmi Zainudin picture Zulhilmi Zainudin · Aug 13, 2015 · Viewed 17.6k times · Source

I found this Bootstrap Select project and its gem for Rails. I want to implement search in the select tag.

I do inspect element and here is the HTML source:

<select class="selectpicker" data-live-search="true">
  <option>Hot Dog, Fries and a Soda</option>
  <option>Burger, Shake and a Smile</option>
  <option>Sugar, Spice and all things nice</option>
</select>

How do I add data-live-search="true" inside my form select tag?

My form select:

<%= f.select :food_id, options_for_select(Food.all.map{|c| [c.name, c.id]}, f.object.food_id), {}, {class: "form-control selectpicker"} %>

What I've tried:

<%= f.select :food_id, options_for_select(Food.all.map{|c| [c.name, c.id]}, f.object.food_id), {}, {class: "form-control selectpicker", data: "live-search"} %>

But it's not working.

Answer

Зелёный picture Зелёный · Aug 13, 2015

Try:

{class: "form-control selectpicker", "data-live-search" => "true" }