2017-09-05 114 views
2

我想創建一個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> 

回答

2

挖掘完後,我發現需要傳遞給包裝配置的相關選項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