SimpleForm 2:包装器内的输入标签

时间:2012-02-28 18:27:55

标签: ruby-on-rails ruby-on-rails-3

使用默认的simple_form 2包装器,它会生成一个如下所示的标记:

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

我想得到一个标记,其中输入标记本身就在这样的包装器中:

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

我是否必须为此行为创建自定义组件?

感谢。

尼古拉斯。

1 个答案:

答案 0 :(得分: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%>

希望这有帮助。