如何为Ruby on Rails collection_select添加bootstrap的表单控件类

时间:2018-05-18 09:04:58

标签: ruby-on-rails ruby bootstrap-4

我有一个collection_select标记,用户可以在其中选择要为故障单分配的Web用户。 collection_select上的项目将来自数据库。

如何将引导程序的form-control类添加到此collection_select标记中?

我试过这段代码:

<%= f.collection_select :web_user_id, WebUser.all,:id,:email, include_blank: true, class: 'form-control' %>

但它没有用。我也阅读了这些Ruby on Rails 5.2.0 documentation,但这没有用。

enter image description here

3 个答案:

答案 0 :(得分:1)

<%= f.collection_select :web_user_id, WebUser.all,:id,:email, {prompt: "Select web users"}, { class: 'form-control' } %>

试试这可能对你有帮助。

答案 1 :(得分:0)

将您的代码转换为:

<%= f.collection_select :web_user_id, WebUser.all,:id,:email, {}, { :include_blank => true, class: 'form-control' } %>

该方法的html_options的一部分。有关更多信息,请参阅https://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

您需要先将options = {}设置为空括号,至少{}

然后,您可以在第二个大括号(include_blank)之间设置classhtml_options = {}

最终表格应该包括{}, { :include_blank => true, class: 'form-control' }

答案 2 :(得分:0)

只需在{}之后添加:email

<%= f.collection_select :web_user_id, WebUser.all, :id, :email, {}, include_blank: true, class: 'form-control' %>

并查看生成的HTML

<select include_blank="true" class="form-control" name="web_user[web_user_id]" id="web_user_web_user_id">
    <option value="20">avcd@example.com</option>
    <option value="21">sa@example.com</option>
</select>