Ruby on Rails 4 - simple_form 多选输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22159614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails 4 - simple_form multiple select input
提问by Travis Glover
I have a simple_form input field that looks like this:
我有一个 simple_form 输入字段,如下所示:
<%= f.input :particular_users, collection: @all_users, input_html: { class: 'multiselectuser', multiple: true} %>
When I leave multiple: true off, the form submits the chosen value for the parameter :particular_users and I can see the value when debugging using "raise params.inspect". However when I leave the multiple: true option there, no vales get passed for the parameter :particular_users.
当我关闭 multiple: true 时,表单会提交参数 :particular_users 的选定值,我可以在使用“raise params.inspect”进行调试时看到该值。但是,当我将 multiple: true 选项留在那里时,参数 :particular_users 不会传递任何值。
What am I doing wrong?
我究竟做错了什么?
EDIT:I can not use the association input because :particular_users is a virtual attribute and has no relationship. I want the multiple select box to pass whatever values that are in there, even if they are arbitrary.
编辑:我不能使用关联输入,因为 :particular_users 是一个虚拟属性并且没有关系。我希望多选框传递其中的任何值,即使它们是任意的。
采纳答案by Travis Glover
It actually does work the way I wanted it to. The trick is to tell the strong parameters to allow a hash. It doesn't throw a strong parameters error, the param just gets thrown out and doesn't come through. So I set it to for example: params.require(:survey).permit(:particular_users => []).
它确实按照我想要的方式工作。诀窍是告诉强参数允许散列。它不会抛出一个强参数错误,参数只是被抛出并且没有通过。所以我将它设置为例如:params.require(:survey).permit(:particular_users => [])。
回答by user3177153
f.input :days, collection: @your_collection, input_html: { multiple: true }
回答by Martin M
To create multiple select tags with simple_form, use:
要simple_form使用来创建多个选择标签,请使用:
<%= f.association :particular_users, collection: @all_users, input_html: { class: 'multiselectuser'} %>
see part Associationsin the gem description.
看到部分Associations的宝石描述。
But as you don't want to use an ActiveRecord associaion, use select_tag:
但由于您不想使用 ActiveRecord 关联,请使用select_tag:
<%= select_tag 'particular_users',
options_from_collection_for_select(@all_users, :id, :name),
multiple: true, class: 'multiselectuser' %>

