f select multiple select is not working using in form

Viewed 652

I have working on project using ruby on rails. I have created a form with many fields and one field is for jobs where I want to choose more then one job but now I am not able to choose more then one job with following code. Working with ruby on rails 3 and HAML. I also mention multiple true but still its not working.

= f.select :jobs, options_from_collection_for_select(Demojob.all, 'name','name' ), :multiple => true 
1 Answers

If you check the implementation of the form builder select method (github), you'll see that the method signature is:

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

multiple flag should be passed using html_options hash, not options. In your case, it should be:

f.select :jobs, options_from_collection_for_select(Demojob.all, 'name', 'name'), {}, :multiple => true

Or, even better, if you prefer new hash syntax:

f.select :jobs, options_from_collection_for_select(Demojob.all, 'name', 'name'), {}, multiple: true

Lastly, there's no need to use options_from_collection_for_select with form builder, you can simply pass the options as arrays:

f.select :jobs, Demojob.all.collect { |job| [job.name, job.name] }, {}, multiple: true

Cheers!

Related