5

SimpleForm + Bootstrap を使用しています。type="text"class = を使用してすべての入力に属性を追加するにはどうすればよいspan12ですか?

次のようなものを出力するもの:

<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

2 に答える 2

6

string_input.rb クラス ファイルを Rails ロード パス (つまり、app/inputs/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

于 2013-04-23T20:44:29.943 に答える
4

私は同様の問題を抱えていましたが、調査の結果、これはラッパー API を使用してサポートされていないことがわかりました。

あなたができる最善の方法は、すべての入力に追加するのではなく、各フォームで :defaults オプションを使用することです。

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

これは次のように実現できます。simple_form_for(@my_instance, defaults: { input_html: { class: 'span12' }})

カスタム フォームビルダーの使用を選択することもできます。 https://github.com/plataformatec/simple_form#custom-form-b​​uilder

于 2013-03-08T11:11:50.723 に答える