3

I have a list of items that I want to have as options for a variable. They will be saved in the model as an array, and are to be displayed as a list in the form_for. I was using

f.select(:var_name, [["option1"],["option2"],["option3"]], {}, {multiple: "multiple"})

Which works great to save into the model. But when going back to the form, nothing is selected (even if the variable has them all saved). Then if I submit the form again, it passes an empty array. The only way for it to save correctly is to re-select the ones I want every time I view the form. How can I get them to pass into the multi-select box?

Josh Johnson
  • 563
  • 3
  • 11
  • 29

2 Answers2

0

I believe your problem stems from your choices parameter. You probably need an array of [option,id] mappings:

f.select(:person_id, Person.all.collect {|p| [ p.name, p.id ] }, {}, { :multiple => true })
kwarrick
  • 5,930
  • 2
  • 26
  • 22
0

When I started working on it again today, it was working. I'm not sure what change was made, but it could be that I needed to restart the server. It still looks like

f.select(:name, [[" "],["option"],["option2"],["option3"]], {}, {:multiple => true})

So it must not have been this code. In addition, the form beginning looks like

form_for(@model_name) do |f|

which hasn't changed either.

Regardless, it works now. Thanks!

Josh Johnson
  • 563
  • 3
  • 11
  • 29