Ruby on Rails 4 - simple_form 多选输入 [英] Ruby on Rails 4 - simple_form multiple select input
问题描述
我有一个像这样的 simple_form 输入字段:
I have a simple_form input field that looks like this:
<%= f.input :particular_users, collection: @all_users, input_html: { class: 'multiselectuser', multiple: true} %>
当我关闭 multiple: true 时,表单会提交参数 :particular_users 的选定值,我可以在使用raise params.inspect"进行调试时看到该值.但是,当我将 multiple: true 选项留在那里时,参数 :particular_users 不会传递任何值.
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.
我做错了什么?
我不能使用关联输入,因为 :particular_users 是一个虚拟属性并且没有关系.我希望多重选择框传递其中的任何值,即使它们是任意的.
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.
推荐答案
它确实按照我想要的方式工作.诀窍是告诉强参数允许散列.它不会抛出一个强参数错误,参数只是被抛出并且没有通过.所以我将它设置为例如:params.require(:survey).permit(:particular_users => []).
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 => []).
这篇关于Ruby on Rails 4 - simple_form 多选输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!