How to align input and label from collection_check_boxes?

后端 未结 1 713
清歌不尽
清歌不尽 2021-02-09 22:15

I am using collection_check_boxes and have problems with aligning checkbox and text. This is my code:

<%= f.coll
1条回答
  •  失恋的感觉
    2021-02-09 22:37

    The definition for collection_check_boxes:

    collection_check_boxes(object, method, collection, value_method, text_method, options = {}, html_options = {}, &block)
    

    The last argument permits you to do something like this: (this does exactly what you want using collection_check_boxes)

    <%= f.collection_check_boxes(:dog_ids, Dog.all, :id, :name) do |b| %>
      
    <%= b.label(class: "check_box") do %>
    <%= b.check_box(class: "check_box") %>
    <%= b.object.name %>
    <% end %>
    <% end %>

    Read more about collection_check_boxes

    There is another method too: style your checkbox input and your label from css.

    For a better css specificity I will add a new class named 'checkbox-list' to:

    <%= f.collection_check_boxes :dog_ids, Dog.all, :id, :name %>
    .checkbox-list input[type="checkbox"] { display: inline-block; width: 10%; } .checkbox-list input[type="checkbox"] + label { display: inline-block; margin-left: 10%; width: 80%; }

    0 讨论(0)
提交回复
热议问题