How do I add HTML attributes to select options with Simple Form Rails?

前端 未结 5 1490
天涯浪人
天涯浪人 2020-12-29 20:08

I need to add a custom HTML attribute to each option for a select control. I\'m using simple_form in Rails. Does anyone know how to do this? The at

5条回答
  •  感动是毒
    2020-12-29 20:55

    You're close! easiest way is actually not using simple_form here. here's the simple_form documentation

    <% options = @group.map { |g| [g.name, g.id, {'data-type' => g.group_type}] } %>
    <%= f.input :group, label: 'Group' do %>
      <%= f.select :group, options, include_blank: 'Select a Group', class: 'form-control' %>
    <% end %>
    

    For your exact code it would be:

    <% options = @group.map { |g| [g[0], g[1], {'data-type' => g[2]}] } %>
    <%= f.input :group, label: 'Group' do %>
      <%= f.select :group, options, include_blank: 'Select a Group', class: 'form-control' %>
    <% end %>
    

提交回复
热议问题