Chris Mendla
Chris Mendla

Reputation: 1017

Rails select tag multiple = true isn't working

I want a select tag to allow multiple selections. I've tried various multiple options but none seem to work. Here is what I have now (Ruby 2.x, Rails 4.x)

  <div class="field">
    <%= f.label :category %><br>
    <%= f.select :category, options_for_select(@categories.sort),
:include_blank => true, :multiple => true  %>
  </div>

When I go to the form, the items are listed, but I can't select multiple items with the Control or Shift keys.

My searches model is

 has_many :documents

  def search_documents

    documents = Document.all

    documents = documents.where("document_title like ?", "%#{document_title}%") if document_title.present?
    documents = documents.where("summary like ?", "%#{summary}%") if summary.present?
    documents = documents.joins(:category).where("categories.name like ?", "%#{category}%") if category.present?
    documents = documents.joins(:owner).where("owners.name like ?", "%#{owner}%") if owner.present?
    documents = documents.where("doc_file_file_name like ?", "%#{file_name}%") if file_name.present?


    return documents
  end

If I view the source of the resulting page, it doesn't appear that the multiple is working

<div class="field">
    <label for="search_category">Category</label><br>xxx
    <select name="search[category]" id="search_category"><option value=""></option>
<option value="Apples">Apples</option>
<option value="Calendar">Calendar</option>
<option value="Cct Catalogs">Cct Catalogs</option>
<option value="Forms">Forms</option>
<option value="Sell Sheets">Sell Sheets</option></select>
  </div>

I believe I will have a problem getting the search to work with multiple items but right now, I am just trying to solve the multi select in the dropdown issue.

Upvotes: 0

Views: 4665

Answers (1)

Mihai Dinculescu
Mihai Dinculescu

Reputation: 20033

Change

<%= f.select :category, options_for_select(@categories.sort),
    :include_blank => true, :multiple => true  %>

to

<%= f.select :category, options_for_select(@categories.sort), 
    {:include_blank => true}, {:multiple => true} %>

All Rails options needs to be defined in a single hash, and all html_options needs to be defined in a separate, single, hash.

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

Upvotes: 4

Related Questions