我还在几个项目中使用 select2 和 Bootstrap。 Select2 有自己的风格并在引导后应用。我认为您必须使用一些 CSS 边距、填充等。您可以为您的 select_tag 提供特定的类以获得更好的定位:
<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3]] ), {class: "my-specific-class-for-select2"}) %>
documentation
编辑:使用数据实时搜索
<%= select_tag(:option, options_for_select([['All', 1], ['Co', 2], ['Bought', 3]] ), {class: "my-specific-class-for-select2", data:{live_search: "true"}) %>
标签将获得 data-live-search 属性。请注意,下划线 (_) 将变为 -。
Documentation about the tags and data attributes.
EDIT2:
通过对 html 和 css 的一些调整,我得到了这个工作:
application.scss
/*Solution for bootsrap styling compliance*/
a.select2-choice{
height: 35px !important;
span{
padding-top: 4px;
}
}
In the view
<div class="form-group clearfix">
<%= f.label :title %><br>
<div class="col-sm-4"><%= f.text_field :title, {class: "form-control"} %></div>
<div class="col-sm-3"><%= select_tag(:type,
options_for_select([['All', 1], ['Co', 2], ['Bought', 3], ['View', 4], ['Top API', 5]] ), {style: "width:100%;"}) %></div>
</div>
<div class="actions clearfix">
<%= f.submit :class => "btn btn-primary" %>
</div>
希望对您有所帮助。