How to add data attribute in Rails form select tag?

20,249

Solution 1

Try:

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

Solution 2

<%= 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}} %>

Solution 3

  <%= 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%>
Share:
20,249

Related videos on Youtube

Zulhilmi Zainudin
Author by

Zulhilmi Zainudin

Hi, welcome to my profile. I wore many hats in the past; full stack web developer, Ethereum blockchain developer and also DevOps developer. Now, my main area of interest are cloud, automation, DevOps and Kubernetes. Thank you for visiting my profile. Feel free to tweet me and say hi! → https://twitter.com/zulhhandyplast

Updated on October 26, 2020

Comments

  • Zulhilmi Zainudin
    Zulhilmi Zainudin over 3 years

    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.

    • Philidor
      Philidor over 8 years
      Have you tried {class: "form-control selectpicker", "data-live-search" => "true" }?
    • Nick Veys
      Nick Veys over 8 years
      Or, data: { 'live-search' => 'true' }.
    • Zulhilmi Zainudin
      Zulhilmi Zainudin over 8 years
      {class: "form-control selectpicker", "data-live-search" => "true"} working. But the search part is not working now. Weird.
    • Philidor
      Philidor over 8 years
      What is search part?
    • Zulhilmi Zainudin
      Zulhilmi Zainudin over 8 years
      Now I have this: <select class="form-control selectpicker" data-live-search="true" name="order[foods_orders_attributes][0][food_id]" id="order_foods_orders_attributes_0_food_id"><option selected="selected" value="7">Nasi Ayam</option> <option value="9">Kebab</option></select> But it's not working.
    • Philidor
      Philidor over 8 years
      What is exactly not working?
    • Zulhilmi Zainudin
      Zulhilmi Zainudin over 8 years
      I have this helper: gist.github.com/anonymous/f0d0666b9ef543ec1ffa. The search functionality only works on saved record. But when I try to add a new record, it's not working. See this - i.imgur.com/h6NAE9O.gif // Both saved & new select tag have the data-live-search="true".
    • Philidor
      Philidor over 8 years
      i assume you should reinitialize your js, because it's new element and selectpicker not initialized for it.
    • Philidor
      Philidor over 8 years
      try it out in browser console after append first input as like in your screen i.imgur.com/h6NAE9O.gif, run in browser console $('.selectpicker').selectpicker(); and check again, let me know if it works.
    • Zulhilmi Zainudin
      Zulhilmi Zainudin over 8 years
      It works. Ok, what should I do now?
    • Philidor
      Philidor over 8 years
  • gshilin
    gshilin over 6 years
    I vote up, because it is full answer with correct example, not just some partial code
  • Aboozar Rajabi
    Aboozar Rajabi over 6 years
    Thanks a lot. After one day searching, it solves my problem! ;)