twitter-bootstrap 如何在 Rails 表单选择标签中添加数据属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31993695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 23:03:29  来源:igfitidea点击:

How to add data attribute in Rails form select tag?

ruby-on-railsrubytwitter-bootstrapruby-on-rails-4bootstrap-select

提问by Zulhilmi Zainudin

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

我找到了这个Bootstrap Select项目及其用于 Rails 的 gem。我想在选择标签中实现搜索。

I do inspect element and here is the HTML source:

我确实检查了元素,这里是 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>

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

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

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.

但它不起作用。

回答by Зелёный

Try:

尝试:

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

回答by un_gars_la_cour

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

回答by gilcierweb

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