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

Viewed 839

The data attribute does not get rendered on this select tag. Any idea what I'm missing?

=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"}

Even a class attribute does not get rendered:

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

This is the output, in both cases:

<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>

If I move the data attributes or class on another (non-select) tag, they get rendered.

Latest versions of both Rails and slim-lang

The documentation shows that starting with the third parameter it should be possible to specify html attributes https://api.rubyonrails.org/v6.0.3.2/classes/ActionView/Helpers/FormTagHelper.html#method-i-select_tag

1 Answers

As per the method definition:

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

You need to wrap the include_blank: true into {} otherwise they're used ad the options method argument and not the 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" }

The same with the class attribute, it should go right into the html_options:

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