Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add data attribute in Rails form select tag?

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.

like image 531
Zulhilmi Zainudin Avatar asked Aug 13 '15 16:08

Zulhilmi Zainudin


3 Answers

Try:

{class: "form-control selectpicker", "data-live-search" => "true" }
like image 192
Philidor Avatar answered Oct 17 '22 12:10

Philidor


<%= 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": true}} %>
like image 40
un_gars_la_cour Avatar answered Oct 17 '22 14:10

un_gars_la_cour


  <%= f.select(:plan_id, {},{}, {'v-model'=>"plan_id",'@change'=>"onChange", class: "form-control"}) do  %>
<% Plan.all.each do |plan| %>
    <%= content_tag(:option,"#{plan.name.upcase} #{plan.max_items}  #{number_to_currency(plan.price)}", value:plan.id, :data => {items: plan.max_items, price: plan.price}) %>
<% end%>
<% end%>
like image 3
gilcierweb Avatar answered Oct 17 '22 12:10

gilcierweb