2013-02-14 89 views
2

我已經爲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

您可以在包裝標籤名稱後添加: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)} 
+2

我曾試圖在此之前,以及在技術上我想: ba.wrapper:標籤=>「格」,:類=> 'switch',:data => {:id =>'as',:label =>'label'} do | box | 結束 我現在試過你的方式來驗證它不是參數的順序,但它仍然沒有工作。我想知道它是否與simple_form_bootstrap以某種方式覆蓋它有關? – Gram 2013-02-15 02:39:22

相關問題