Rails + slim:select 标签上的数据属性未呈现

Rails + slim: Data attributes on a select tag not getting rendered

此 select 标记上未呈现数据属性。知道我错过了什么吗?

=f.select :template_kind, options_for_select(f.object.class::TEMPLATE_KINDS, selected: f.object.template_kind), include_blank: true, data: {target: "template-form.switch", action: "change->template-form#handleChange"}

即使 class 属性也不会呈现:

=f.select :template_kind, options_for_select(f.object.class::TEMPLATE_KINDS, selected: f.object.template_kind), include_blank: true, class: "myclass"

这是两种情况下的输出:

<select name="system_template[template_kind]" id="system_template_template_kind">
<option value=""></option>
<option value="email_message">email_message</option>
<option selected="selected" value="page">page</option>
</select>

如果我将数据属性或 class 移动到另一个(非 select)标签上,它们将被渲染。

Rails 和 slim-lang

的最新版本

文档显示从第三个参数开始应该可以指定 html 属性 https://api.rubyonrails.org/v6.0.3.2/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag

根据方法定义:

select(object, method, choices = nil, options = {}, html_options = {}, &block) 

您需要将 include_blank: true 包装到 {} 中,否则它们将用于 options 方法参数而不是 html_options:

f.select :template_kind,
         options_for_select(f.object.class::TEMPLATE_KINDS, selected: f.object.template_kind),
         { include_blank: true },
         data: { target: "template-form.switch", action: "change->template-form#handleChange" }

与 class 属性相同,它应该直接进入 html_options:

...,
  { include_blank: true },
  { class: "myclass",
    data: { target: "template-form.switch",
            action: "change->template-form#handleChange" } }