Select multiple options in rails collection_select

I know how to assemble a simple selection block that takes values ​​from a model

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

My question is how can I allow the user to select several parameters and then save them in the model. I know what I need to use

 :multiple => true 

But not sure about the syntax

Usually for several records in the model I would use accepts_nested_attributes_for, but I correctly understood that I do not need this for an example?

thanks

+7
ruby ruby-on-rails-3
source share
2 answers

Good after some trial and error.

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

allows me to select multiple options

+12
source share
 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

What is included in the hash parameters is described at the top of the page and includes: a prompt. The html_options hash is for the html attributes you want to set, for example. multiple, class, id.

0
source share

All Articles