我为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>
答案 0 :(得分:1)
@Learner's answer已关闭。诀窍是要包含html
哈希:
ba.wrapper tag: 'div',
html: { data: { id: 'something', label: 'blah' },
class: 'switch' do |box|
box.use :input
end
注意:此方法适用于<builder>.wrapper
方法,但不适用于config.wrappers
,<builder>.use
等。
答案 1 :(得分:0)
您可以在包装器
中的标记名称后面添加:data => {:id=> 'something', :label => 'blah'}
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', :data => {:id=> 'something', :label => 'blah'}, :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
即使您可以添加名称,也可以添加以下值:
:data => {:name => 'Stephen', :city_state => %w(Chicago IL)}