Style Rails collection_check_boxes



我一直试图将一些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>

我该怎么做呢?

试一下:

    <%= 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 %>

使用内联块稍微短一点

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

最新更新