2012-02-28 58 views
5

裏面輸入標籤使用默認simple_form 2的包裝,它會生成一個標記,看起來像這樣:SimpleForm 2:包裝

<div class="..."> 
    <label>...</label> 
    <input ... /> 
</div> 

我想獲得一個標記,其中輸入的標籤是自身內部一個這樣的包裝:

<div class="..."> 
    <label>...</label> 
    <div class="..."> 
    <input ... /> 
    </div> 
</div> 

我是否必須爲此行爲創建一個自定義組件?

謝謝。

Nicolas。

回答

7

檢查了這一點:

的封裝API是不是有據可查的,但我花了一些時間,試圖弄明白。這裏有一個簡單的例子,它可以在simple_form初始化器中設置。

# Use this setup block to configure all options available in SimpleForm. 
SimpleForm.setup do |config| 
    # Wrappers are used by the form builder to generate a complete input. 
    # You can remove any component from the wrapper, change the order or even 
    # add your own to the stack. The options given to the wrappers method 
    # are used to wrap the whole input (if any exists). 

    config.wrappers :GIVE_YOUR_WRAPPER_A_NAME, :class => 'clearfix', :error_class => nil do |b| 
    b.use :placeholder 
    b.use :label 
    b.use :tag => 'div', :class => 'WHATEVER_YOU_WANT_TO_CALL_IT' do |ba| 
     ba.use :input 
     ba.use :error, :tag => :span, :class => :'help-inline' 
     ba.use :hint, :tag => :span, :class => :'help-block' 
    end 
    end 
end 

然後,當你創建一個表單只是指定要使用包裝:

<%= simple_form_for @user, wrapper: 'WHATEVER_YOU_CALLED_YOUR_WRAPPER' do |form| %> 
<%end%> 

希望這有助於。

+0

那就是那個!我不知道你可以明確定義「use:input」來控制輸入標籤的位置。這應該寫在文檔中。大! – 2012-02-28 18:44:19