simple_form中的自定义包装:重复输入

时间:2018-06-16 21:07:27

标签: ruby ruby-on-rails-5 simple-form

我正在使用Rails 5.2应用程序中的simple_form gem。目前,我在输入中使用custom wrappers时遇到了一些问题。

我的包装

config.wrappers :mini_input, tag: 'div', class: 'row responsive-label', error_class: 'error' do |b|
  b.use :html5
  b.wrapper tag: 'div', class: 'col-sm-12 col-md-3' do |c|
    c.use :label_input
  end
  b.wrapper tag: 'div', class: 'col-sm-12 col-md-4' do |c|
    c.use :input
  end
end

我的表格

simple_form_for(@user) do |f|
  ...
  <%= f.input :name, required: true, label: "Name", autofocus: true, wrapper: :mini_input %>
  ...
end

预期输出

<div class="row responsive-label">
  <div class="col-sm-12 col-md-3">
    <label ...>
  </div>
  <div class="col-sm-12 col-md-4">
    <input ...>
  </div>
</div>

...实际输出

<div class="row responsive-label">
  <div class="col-sm-12 col-md-3">
    <label ...>
    <input ...>    <!-- This input should not be here -->
  </div>
  <div class="col-sm-12 col-md-4">
    <input ...>
  </div>
</div>

我想也许我的包装器配置中有错误,我现在无法找到它。

1 个答案:

答案 0 :(得分:1)

正如我的一位朋友in a tweet所指出的,解决方案是在包装器配置中使用label而不是label_input

config.wrappers :mini_input, tag: 'div', class: 'row responsive-label', error_class: 'error' do |b|
  b.use :html5
  b.wrapper tag: 'div', class: 'col-sm-12 col-md-3' do |c|
    # c.use :label_input  Wrong
    c.use :label
  end
  b.wrapper tag: 'div', class: 'col-sm-12 col-md-4' do |c|
    c.use :input
  end
end
相关问题