Chris Mendla
Chris Mendla

Reputation: 1017

collection_select in ruby not allowing multi select

I am trying to get a multi select dropdown working in Rails. The code is:

<div class="field">
<%= f.label :tag_id %><br>
<td><%= f.collection_select(:tag_id, Tag.all, :id, :name, {:multiple => true})%></td>

I've also tried

<td><%= f.collection_select(:tag_id, Tag.all, :id, :name, :multiple true)%>

I thought that I should be able to hold the shift or ctrl and click on multiple items to select more than one item

I suspect that the problem could be in the schema for the documents table that is linked to the Tags model. .

  t.integer  "tag_id"

The documents model has

class Document < ActiveRecord::Base
 . . .
   belongs_to :tag
  . . . 
end

and the tag model has has_many_documents

  1. Does the multi actually work with f.collection_select
  2. Should I be using something other than 'belongs_to :tag?

Upvotes: 0

Views: 70

Answers (1)

Pavan
Pavan

Reputation: 33552

The below should work

<%= f.collection_select(:tag_id, Tag.all, :id, :name, {}, {:multiple=> true})%>

You should pass it as the last argument(i.e, html_options = {}), but currently you are passing it in options = {}.

Upvotes: 1

Related Questions