【问题标题】:Style Rails collection_check_boxes样式 Rails collection_check_boxes
【发布时间】:2014-07-16 00:51:15
【问题描述】:

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

关于如何做到这一点的任何想法?

【问题讨论】:

    标签: html css ruby-on-rails


    【解决方案1】:

    试试这样:

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

    使用内联块要短一些

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-11
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多