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

﹥>﹥吖頭↗ 提交于 2019-12-21 06:49:15

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/31373730/how-to-post-a-collection-of-ids-with-multiple-select-form-field-with-phoenix-htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!