Search code examples
ruby-on-railsselectruby-on-rails-5form-helpers

Select multiple using Rails `#select` form helper


I've got a fairly simple (it seems) bit of code to create a multiple-selection element:

<%= form_for @post do |f| %>  
  #stuff

  <%= fields_for :tags |tag_fields| %>
    <%= tag_fields.label :select_tags %>
    <%= tag_fields.select :tags, Tag.all, multiple: true %>
  <% end %>
<% end %>

For some reason when it renders the page, the multiple: true part isn't getting parsed, and the form is just rendering as a generic dropdown with only one option selectable. What am I missing?

(Rails version is 5.0.0 in case relevant)


Solution

  • The signature of the select helper is:

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

    multiple is an HTML option, therefore you should use:

    <%= tag_fields.select :tags, Tag.all, {}, multiple: true %>