我为 simple_form 创建了一个自定义包装器,但似乎找不到将数据属性添加到生成的包装器元素的方法。我正在尝试将其添加到名为 switch 的内部包装类中。
我希望能够将它添加到包装器中,如果可能的话不在视图层中。
config.wrappers :toggle, :tag => 'div', :class => 'control-group', :error_class => 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper :tag => 'div', :class => 'controls' do |ba|
ba.wrapper :tag => 'div', :class => 'switch' do |box|
box.use :input
end
ba.use :hint, :wrap_with => { :tag => 'span', :class => 'help-block' }
ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
end
end
= form.input :element, :label => t('views.items.attributes.element'), :wrapper => :toggle
输出
<div class="control-group boolean optional">
<label class="boolean optional control-label" for="item_element">Element</label>
<div class="controls">
<div class="switch">
<input name="item[element]" type="hidden" value="0">
<input class="boolean optional" id="item_element" name="item[element]" type="checkbox" value="1">
</div>
</div>
</div>
期望的输出
<div class="control-group boolean optional">
<label class="boolean optional control-label" for="item_element">Element</label>
<div class="controls">
<div class="switch" data-label="blah" data-id="something">
<input name="item[element]" type="hidden" value="0">
<input class="boolean optional" id="item_element" name="item[element]" type="checkbox" value="1">
</div>
</div>
</div>