如何使用phoenix_html发布具有多个选择表单字段的ID集合 [英] How to post a collection of ids with multiple select form field with phoenix_html
问题描述
我正在尝试通过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: "" %>
但只有最后选择的项目的ID会通过参数发送到服务器
but only the id of the last selected item gets sent to the server in the params
%{"challenge_ids" => "3", "content" => "", "name" => ""}
我也曾尝试将:challeng_ids
更改为:"challenge_ids[]"
,以尝试获得类似于多选择标签的rails输出的功能,但这并没有什么不同
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
推荐答案
亚伦的用于添加多个选择的PR 已合并进入phoenix_html.这是multiple_select/4
的 docs 中的示例,以防其他人偶然发现相同的内容问题:
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>
这篇关于如何使用phoenix_html发布具有多个选择表单字段的ID集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!