如何在 Rails 表单 select 标签中添加数据属性?

How to add data attribute in Rails form select tag?

我找到了这个 Bootstrap Select project and its gem for Rails。我想在 select 标签中实现搜索。

我会检查元素,这里是 HTML 来源:

<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>

如何在我的表单 select 标签中添加 data-live-search="true"

我的表格select:

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

我尝试过的:

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

但是没用。

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

尝试:

{class: "form-control selectpicker", "data-live-search" => "true" }
  <%= 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%>

如果您要将块传递给 select,则需要传递 {} 两次,例如

    => f.select :to_user_id, {}, {}, data: { placeholder: "Choisis des invités..." }, class: "chosen-image" do
      - User.find_each do |user|
        option(data-img-src="#{user_image_url_for(user)}" value="#{user.id}")
          = user.first_name