Richlewis
Richlewis

Reputation: 15384

Select multiple options in a collection_select rails

I know how to put together a simple select box that takes its values from a model

<%= f.collection_select(:sector_id, Sector.all, :id, :name, :prompt => "Please Select a Sector") %>

My question is how do i allow a user to select multiple options and then store them in the model. I know i need to use

:multiple => true

But unsure on the syntax

Usually for multiple entries to a model i would use accepts_nested_attributes_for but am i correct in thinking i don't need to for this example?

Thanks

Upvotes: 15

Views: 16312

Answers (2)

7stud
7stud

Reputation: 48649

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

The things that go in the options hash are described at the top of the page, and include :prompt. The html_options hash is for html attributes that you want to set, e.g. multiple, a class, an id.

Upvotes: -1

Richlewis
Richlewis

Reputation: 15384

Ok after some trial and error

<%= f.collection_select(:sector_id, Sector.all, :id, :name, {:prompt => "Please Select a Sector"}, {:multiple => true}) %>

lets me select multiple options

Upvotes: 27

Related Questions