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

Nic*_*nco 5 ruby-on-rails ruby-on-rails-3

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

<div class="...">
  <label>...</label>
  <input ... />
</div>
Run Code Online (Sandbox Code Playgroud)

我想得到一个标记,其中输入标记本身在一个包装器中,如下所示:

<div class="...">
  <label>...</label>
  <div class="...">
    <input ... />
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

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

谢谢.

尼古拉斯.

The*_*hop 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
Run Code Online (Sandbox Code Playgroud)

然后,在创建表单时,只需指定要使用的包装器:

<%= simple_form_for @user, wrapper: 'WHATEVER_YOU_CALLED_YOUR_WRAPPER' do |form| %>
<%end%>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.