对某些单选按钮应用一个 simple_form 自定义包装,但对其他单选按钮应用不同的包装

Apply one simple_form custom wrapper to some radio buttons, but a different wrapper for other radio buttons

我们希望将第一个单选按钮包装应用到大多数使用 simple_form 的收音机。我们已经在 simple_form 配置中这样做了。

config.wrapper_mappings = {
 radio_buttons: :wrapped_radio_buttons
}

第一电台class:

config.wrappers(
 :wrapped_radio_buttons,
 class: "field-unit radio-buttons",
 error_class: "field-with-errors"
) do |b|
 b.use :error, wrap_with: { tag: "span", class: "field-error" }
 b.use :label_input
end

我们想对其他一些无线电应用第二个无线电包装器:

config.wrappers(
 :wrapped_radio_blocks,
 class: "field-unit radio-buttons radio-block-group",
 error_class: "field-with-errors"
) do |b|
  b.use :label
  b.use :error, wrap_with: { tag: "span", class: "field-error" }
  b.wrapper tag: "div", class: "radio-blocks" do |ba|
   ba.use :input
  end
end

我们尝试为第二个选项添加一个配置包装器映射,但似乎只能为每个 html 输入类型应用一个包装器。我们如何将第二个包装器应用于某些收音机?

这是我们的 html:

<%= f.input(
  :fake_input,
  as: :radio_buttons,
  collection: t(
    [
      :radio_option_one,
      :radio_option_two,
      :radio_option_three,
      :radio_option_four,
      :radio_option_five,
    ],
    scope: "fake_scope",
  )
) %>

我们想用第二个包装器类型的不同行替换 as: :radio_buttons

好吧,我们在发布后不久就使用 collection_wrapper_tag and collection_wrapper_class 解决方法解决了这个问题,而不是创建一个全新的 simple_form 自定义包装器。

<%= f.input(
  :regular_radio_example,
  as: :radio_buttons,
  collection: t(
    [
      :radio_option_one,
      :radio_option_two,
      :radio_option_three,
      :radio_option_four,
      :radio_option_five,
    ],
    scope: "fake_scope",
  ),
) %>

<%= f.input(
  :new_cool_radio_example,
  as: :radio_buttons,
  wrapper_html: { class: "radio-block-group" },
  collection: t(
    [
      :radio_option_one,
      :radio_option_two,
      :radio_option_three,
      :radio_option_four,
      :radio_option_five,
    ],
    scope: "fake_scope",
  ),
  collection_wrapper_tag: "div",
  collection_wrapper_class: "radio-blocks",
) %>