Aaron Dufall
Aaron Dufall

Reputation: 1177

How to post a collection of ids with multiple select form field with phoenix_html

I’m trying to get multiple select to work with the phoenix_html form helpers

<%= select f, :challenge_ids, ["foo": "1","bar": "2","baz": "3"], class: "form-control", multiple: ""  %>

but only the id of the last selected item gets sent to the server in the params

%{"challenge_ids" => "3", "content" => "", "name" => ""}

I have also tried changing :challeng_ids to :"challenge_ids[]" trying to get something similar to a rails output for a multiple select tag, but this didn't make any difference

Upvotes: 25

Views: 4327

Answers (1)

Nithin
Nithin

Reputation: 314

Aaron's PR for adding multiple_select was merged into phoenix_html. Here's an example from the docs for multiple_select/4 in case someone else stumbled across the same problem:

# Assuming form contains a User model
multiple_select(form, :roles, ["Admin": 1, "Power User": 2])
#=> <select id="user_roles" name="user[roles][]">
    <option value="1">Admin</option>
    <option value="2">Power User</option>
    </select>

Upvotes: 8

Related Questions