引导轨collection_select和date_select

时间:2019-02-14 19:39:38

标签: ruby-on-rails twitter-bootstrap

在我的Rails应用程序上,我在表单上有一个collection_select和date_select,但是找不到有关如何添加bootstrap类以自定义字段的任何信息。

<div class="field form-group">
  <%= form.label :exercise_id %>
  <%= form.collection_select(:exercise_id, Exercise.all, :id, :name, prompt: true, {}, {:class => 'form-control'}) %>
</div>

<div class="field form-group">
  <%= form.label "Date" %>
  <%= form.date_select :activitydate, {}, {:class => 'form-control'} %>
</div>

由于collection_select语法,我得到一个错误。 date_select语法有效,但我想自定义生成的各个标签。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

以下是带有导轨的引导程序4的示例代码,如果您使用引导程序3,只需将col-form-label更改为form-label

通常collection_select用于多个选择,这就是使用 arrayexercise_ids multiple:true 的原因,如下所示

<div class="row form-group">
  <%= form.label "Choose Exercises", :class => 'col-form-label col-sm-4' %>
  <div class="col-sm-6">
    <%= form.collection_select :exercise_ids, Exercise.all, :id, :name, {}, { multiple: true, class: 'form-control' } %>
  </div>  
</div>

如果您打算选择一种锻炼方式(个人建议),请使用select

<div class="row form-group">
  <%= form.label "Choose Exercises", :class => 'col-form-label col-sm-4' %>
  <div class="col-sm-6">
    <%= f.select :exercise_id, Exercise.all, { include_blank: true } , { class:  'form-control' }  %>
  </div>  
</div>

对于日期助手,您可以check here for reference,以便对其进行自定义

相关问题