Rails style collection_check_boxes

I am trying to apply some CSS classes to collection_check_boxes, but I cannot get it to work. I'm doing it now:

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

which outputs this 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> 

Instead, I want to output this 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> 

I tried the following that does not work ...

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

Any ideas on how I can do this?

+6
source share
2 answers

Try the following:

  <%= 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 %> 
+20
source

Use the built-in unit a little shorter

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

Source: https://habr.com/ru/post/972280/


All Articles