【问题标题】:How to prevent simple_form from applying default label to a custom wrapper?如何防止 simple_form 将默认标签应用于自定义包装器?
【发布时间】:2016-07-06 00:17:34
【问题描述】:

我正在使用 SimpleForm + Bootstrap。我需要在复选框输入后立即添加<i></i>tag,如下:

<div class="form-group">
  <div class="checkbox">
    <label class="checkbox">
      <input type="checkbox" >
      <i></i>
      Label Text
    </label>
  </div>
</div>

所以我修改了初始化器

#initializers/simple_form_bootstrap.rb
config.wrappers :vertical_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
  b.use :html5
  b.optional :readonly

  b.wrapper tag: 'div', class: 'checkbox' do |ba|
    ba.wrapper tag: 'label' do |bb|
      bb.use :input
      bb.wrapper tag: 'i' do |baa|
      end
      bb.use :label_text
    end
  end

  b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
  b.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
end 

但这是插入一个额外的标签标签

<div class="form-group">
  <div class="checkbox">
    <label>
    <label class="checkbox">
      <input type="checkbox" >
      <i></i>
      Label Text
    </label>
    </label>
  </div>
</div>

label.checkbox 是 simple_form 应用的默认标签。即,通过config.boolean_label_class = 'checkbox'。所以看起来我可以删除我的自定义标签包装器。

b.wrapper tag: 'div', class: 'checkbox' do |ba|
  # ba.wrapper tag: 'label' do |bb|
    bb.use :input
    bb.wrapper tag: 'i' do |baa|
    end
    bb.use :label_text
  # end
end

但是标签只围绕输入。

<div class="form-group">
  <div class="checkbox">
    <label class="checkbox">
      <input type="checkbox" >
    </label>
    <i></i>
    Label Text
  </div>
</div>

我已尝试启用和禁用 inline_label。我正在使用一个包含默认 simple_form 包装器的新配置文件。唯一的修改如上。

use label_input 未被调用时,为什么 Simple_Form 对输入应用标签?

如何配置 simple_form 以包装输入、&lt;i&gt; 标记和带有标签的标签文本?

【问题讨论】:

    标签: ruby-on-rails simple-form


    【解决方案1】:

    这让我很头疼,试图弄清楚,但最终有一个简单的解决方法。

    为了其他遇到类似问题的人的利益,请在初始化程序中进行以下更改

    #simple_form.rb
    
    # disable the default :nested boolean style
    # and enable :inline
    config.boolean_style = :nested
    config.boolean_style = :inline
    

    在此更改之后,我的自定义包装器按预期运行,没有“神奇地”出现其他元素。

    【讨论】:

      猜你喜欢
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多