2013-02-20 41 views
4

我正在使用SimpleForm + Bootstrap。我如何添加一個屬性到所有type="text"輸入與類= span12SimpleForm默認輸入類

東西輸出是這樣的:

<div class="controls"><input autofocus="autofocus" class="string required span12" id="user_first_name" name="user[first_name]" required="required" size="50" type="text" value=""></div> 

我試着用config.wrappers打不過這個

ba.use :input, :wrap_with => { :class => 'span12' } 

不起作用。它添加到包裝而不是修改輸入標籤。有什麼想法嗎?

SimpleForm.setup do |config| 
    config.wrappers :bootstrap, :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.use :input 
     ba.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' } 
     ba.use :hint, :wrap_with => { :tag => 'p', :class => 'help-block' } 
    end 
    end 
    config.default_wrapper = :bootstrap 
end 

回答

4

我有一個類似的問題,並經過一些研究後,我發現這不支持使用包裝API。

您可以做的最好的方法是在每個表單中使用:defaults選項,而不是爲每個輸入添加。

https://github.com/plataformatec/simple_form/issues/754

可以這樣實現這一點:simple_form_for(@my_instance, defaults: { input_html: { class: 'span12' }})

您也可以選擇使用自定義formbuilder。 https://github.com/plataformatec/simple_form#custom-form-builder

4

你可以簡單地通過添加string_input.rb類文件到您的軌道負載路徑(即應用程序/輸入/ string_input.rb)覆蓋字符串輸入simple_form默認幷包括如下:

class StringInput < SimpleForm::Inputs::StringInput 
    def input_html_classes 
    super.push('span12') 
    end 
end 

如果你需要更多的默認類添加到輸入的其他類型,你可以檢查出各種輸入類型提供:

https://github.com/plataformatec/simple_form/tree/master/lib/simple_form/inputs

+0

我想這可以作爲一個臨時解決方案,直到配置將允許它。 – clyfe 2013-08-03 13:20:11

+1

對於那些通過Google來到這裏的人,現在從v2.2開始支持(參見[changelog](https://github.com/plataformatec/simple_form/blob/v2.2/CHANGELOG.md)), config.input_class'調用,但是這也會將容器div包裝在同一個類中,這使得引導樣式不可靠。因此,即使配置允許在所有輸入上設置類,這個答案仍然可能是最好的解決方案。 – nickcoxdotme 2014-02-06 00:50:46