Search code examples
ruby-on-railsformssimple-form

rails simple-form add data attributes to input select wrapper as well as select options


I've followed this SO answer to add data attributes to my select options. But I also need a data attribute in my input select wrapper.

Currently, it looks like this:

<% options = @active_users.map do |user| %>
  <% [user.full_name, user.id, data: { 'org': user.organization.name }] %>
<% end %>
<%= f.input :user, label: false, required: false, as: :select,
      wrapper_html: { class: 'search person js-linked',
                      data: { 'linked-by': 'person' }
      } do
%>
  <%= f.select :user_id, options, { include_blank: 'Add user…' },
          { class: 'select optional', placeholder: 'Add user…' }
  %>
<% end %>

If I test the option data attribute without the input wrapper data attribute it works like it's supposed to (the data attribute gets added to the options), but as soon as I add the wrapper_html data attribute, like shown in the code above, the data attributes in the select options disappear.

I also tried using a custom input as suggested in this SO answer, but that yielded the same result.

I'd appreciate any insights into why this is happening and how to solve it.


Solution

  • As it turns out, this was caused by the JS code that is handling the linked-by. It was not considering possible data attributes in the options, they just got overwritten.