Style Rails collection_check_boxes

时间:2014-07-16 00:51:15

标签: html css ruby-on-rails

我一直在尝试将一些CSS类应用到collection_check_boxes,但我无法让它工作。现在我这样做:

<div class="form-group">
    <%= f.collection_check_boxes(:brand_ids, Brand.all, :id, :name) do |b| %>
        <%= b.label { b.check_box + b.text } %>
    <% end %>
</div>

输出此HTML:

<div class="form-group">
    <label for="user_brand_ids_1">
        <input id="user_brand_ids_1" name="user[brand_ids][]" type="checkbox" value="1">Brand 1
    </label>
    <input name="user[brand_ids][]" type="hidden" value=""> 
</div>

相反,我想输出这个HTML:

<div class="form-group">
    <label class="label-checkbox" for="user_brand_ids_1">
        <input id="user_brand_ids_1" name="user[brand_ids][]" type="checkbox" value="1">
        <span class="custom-checkbox"></span>Brand 1
    </label>
    <input name="user[brand_ids][]" type="hidden" value=""> 
</div>

我已尝试过以下内容,但这些内容并不起作用......

<div class="form-group">
    <%= f.collection_check_boxes(:brand_ids, Brand.all, :id, :name, {}, {class: 'label-checkbox'}) do |b| %>
        <%= b.label { b.check_box + b.text }, class: 'label-checkbox' %>
    <% end %>
</div>

关于我怎么能这样做的任何想法?

2 个答案:

答案 0 :(得分:28)

试试这样:

    <%= f.collection_check_boxes(:brand_ids, Brand.all, :id, :name) do |b| %>
        <%= b.label class:"label-checkbox" do%>
         <%=b.check_box + b.text%>
        <%end%>
    <% end %>

答案 1 :(得分:2)

使用内联块稍短一点

<%= f.collection_check_boxes(:brand_ids, Brand.all, :id, :name) do |b| %>
  <%= b.label(class:"label-checkbox") { b.check_box + b.text } %>
<% end %>