3

Using this line of code:

<%= m.input :battery, :collection => ["1","2","3","4"], :input_html => { :multiple => true } %>

I see a perfectly rendered multi-select menu in my form.

However, when I submit the form (after selecting 1 and 2) I receive the following:

1.9.3p374 :012 > Style.find(402).battery
  Style Load (1.8ms)  SELECT `styles`.* FROM `styles` WHERE `styles`.`id` = 402 LIMIT 1
 => "---\n- ''\n- '1'\n- '2'\n" 

Now when I reload the Style edit form, nothing is selected in the multi-select, and if I resubmit, my previous selections are overwritten:

=> "---\n- ''\n" 

Obviously the format being used is not being interpretted correctly by the form

Controller update action

@style = Style.find(params[:id]) 
@style.update_attributes(params[:style])

Params hash

"battery"=>["", "1", "2"]

UPDATE

After adding serialize :battery, Array to my style model I am now seeing:

Style Load (0.1ms)  SELECT `styles`.* FROM `styles` WHERE `styles`.`id` = 402 LIMIT 1
 => ["", "1", "2"] 

Most importantly, the form is loading the DB data now.

0

1 Answer 1

2

If you want to save an array in your battery field u can use

serialize :battery, Array

in model

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.