如何将 SimpleForm 输入配置为默认为无包装器

How to configure a SimpleForm input to default to no wrapper

给定自定义 SimpleForm 输入

class MyCustomInput < SimpleForm::Inputs::Base
  # some stuff
end

我该如何配置才能使这些输入在默认情况下没有包装器。

通常我会像这样设置特定于输入的包装器:

# initializers/simpleform
config.wrapper_mappings = {
  my_custom: :my_wrapper
}

但以下不起作用,仍然应用默认的 SimpleForm 包装器。

config.wrapper_mappings = {
  my_custom: false
}

我知道在视图中有一些方法可以实现这一点,例如

<%= f.input :attribute, as: :my_custom, wrapper: false %>

<%= f.input_field :attribute %>

但这不是我要找的。

有没有一种方法可以配置输入,使默认没有包装器?

除了您在问题中已经提到的选项,仅使用 input_field 而不是 input(此处更多信息:https://github.com/plataformatec/simple_form#stripping-away-all-wrapper-divs),您可以像这样定义包装器(未经测试):

SimpleForm.setup do |config|
  config.wrapper_mappings = {
    my_custom: :wrapper_false,
  }

  config.wrappers :wrapper_false, tag: false do |b|
    b.use :placeholder
    b.use :label_input
    b.use :hint,  wrap_with: { tag: :span, class: :hint }
    b.use :error, wrap_with: { tag: :span, class: :error }
  end
end

然后在您的表单中:

<%= f.input :attribute, as: :my_custom %>

有关包装器的更多信息 API:https://github.com/plataformatec/simple_form#the-wrappers-api

编辑:

考虑到您提到的情况后,I sent a PRsimple_form 项目以允许在 wrapper_mappings 中配置 wrapper: false。如果它被接受,您应该能够像这样在初始化程序中直接禁用包装器:

config.wrapper_mappings = {
  my_custom: false
}