Style Rails collection_check_boxes

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

I been trying to apply some CSS classes to collection_check_boxes but I can't get it to work. Right now I doing this:

<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 would like 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've tried the following, which doesn't 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 could I do this?

回答1:

Try it like this:

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


回答2:

A little shorter to use an inline block

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!