mardi 25 octobre 2016

Rails Form: How to handle multiple selections in drop-down menu

I've been playing around with my form where I have a drop-down menu where you can select multiple options:

f.select :hobbies, [
                    ['First First First First','1'],
                    ['Second Second Second Second','2'],
                    ['Third Third Third Third','3'],
                    ['Fourth Fourth Fourth Fourth','4'],
                    ['Fifth Fifth Fifth Fifth','5'],
                    ['Sixth Sixth Sixth Sixth','6'],
                   ],
                   {},
                   {:multiple => true}

But I'm so confused at what the heck this thing is putting into my hobbies attribute (which is a string).

When I print out the contents after selecting the first three options:

<%= @user.hobbies %>

I get this junk:

--- - '' - '1' - '2' - '3'

So clearly it's getting the '1', '2', and '3' that I selected, which is good. But the rest of the output is weird looking.

All that I want to do is be able to print '1', '2', and '3' (or whatever options were selected). Of course, I could parse these values out of the giant string based on it's dash-separated format, but that seems the wrong way to do it.

I saw something about turning "hobbies" into an array by making it "hobbies[]", but that gives the odd error:

(wrong number of arguments (0 for 1..2))

So how exactly am I supposed to handle this stuff internally?

Aucun commentaire:

Enregistrer un commentaire