自定义simple_form包装匹配Bootstrap 4复选框

自定义simple_form包装匹配Bootstrap 4复选框

问题描述:

我想创建一个simple_form包装匹配Bootstrap 4的堆叠复选框/收音机。自定义simple_form包装匹配Bootstrap 4复选框

这里的HTML结构,我想复制的Boostrap's docs礼貌:

<div class="form-check"> 
    <label class="form-check-label"> 
    <input class="form-check-input" type="checkbox" value=""> 
    Option one is this and that&mdash;be sure to include why it's great 
    </label> 
</div> 
<div class="form-check disabled"> 
    <label class="form-check-label"> 
    <input class="form-check-input" type="checkbox" value="" disabled> 
    Option two is disabled 
    </label> 
</div> 

这里就是我的simple_form包装目前为:

config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| 
    b.use :html5 
    b.optional :readonly 
    b.use :label 
    b.use :input, class: "form-check-input" 
    b.use :error, wrap_with: { tag: 'div', class: 'invalid-feedback' } 
    b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } 
end 

而这里的HTML结构,它目前输出:

<span class="checkbox"> 
    <label for=""> 
    <input class="form-check-input check_boxes optional" type="checkbox" value="" name="" id=""> 
    Text for checkbox goes here 
    </label> 
</span> 

挖掘完后,我发现需要传递给包装配置的相关选项item_wrapper_classitem_wrapper_label

config.wrappers(:vertical_radio_and_checkboxes, tag: 'div', 
    class: 'form-group', 
    error_class: 'has-error', 
    item_wrapper_class: 'form-check', 
    item_label_class: 'form-check-label') do |b| 
    b.use :html5 
    b.optional :readonly 
    b.use :label, class: 'form-control-label' 
    b.use :input, class: 'form-check-input' 
    b.use :error, wrap_with: { tag: 'div', class: 'invalid-feedback' } 
    b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } 
end