如何从collection_check_boxes对齐输入和标签?

Cri*_*ano 6 css checkbox ruby-on-rails twitter-bootstrap ruby-on-rails-4

我正在使用collection_check_boxes并且在对齐复选框和文本时遇到问题.这是我的代码:

<div class="col-md-4">
  <%= f.collection_check_boxes :dog_ids, Dog.all, :id, :name %>
</div>
Run Code Online (Sandbox Code Playgroud)

表单显示如下:

[checkbox1]
  text1

[checkbox2]
  text2

[checkbox3]
  text3
Run Code Online (Sandbox Code Playgroud)

我试图调整输入和标签,但没有成功.我已经看到了这些问题,但它对我不起作用:将f.collection_check_boxes的复选框与Simple_Form对齐

我想完成这个:

[checkbox1] text1

[checkbox2] text2

[checkbox3] text3
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

cri*_*ian 28

定义collection_check_boxes:

collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
Run Code Online (Sandbox Code Playgroud)

最后一个参数允许你做这样的事情:(这正是你想要的使用collection_check_boxes)

<%= f.collection_check_boxes(:dog_ids, Dog.all, :id, :name) do |b| %>
  <div class="row">
    <%= b.label(class: "check_box") do %>
      <div class="col-xs-4">
        <%= b.check_box(class: "check_box") %>
      </div>

      <div class="col-xs-8">
        <%= b.object.name %>
      </div>       
    <% end %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

阅读有关collection_check_boxes的更多信息

还有另一种方法:设置您的复选框输入和来自CSS的标签.

为了更好的css特异性,我将添加一个名为'checkbox-list'的新类:

<div class="col-md-4 checkbox-list">
  <%= f.collection_check_boxes :dog_ids, Dog.all, :id, :name %>
</div>

.checkbox-list input[type="checkbox"] {
  display: inline-block;
  width: 10%;
}

.checkbox-list input[type="checkbox"] + label {
  display: inline-block;
  margin-left: 10%;
  width: 80%;
}
Run Code Online (Sandbox Code Playgroud)