Rails:多个下拉菜单collection_select [英] Rails: Multiple dropdown menus collection_select
问题描述
此处是Super Rails n00b:目前,我有一个包含以下代码的表单:
Super Rails n00b here: Currently I have a form with the following code:
<%= f.collection_select :account_ids, @accounts, :id, :name, include_blank: true %>
,它目前可以按我的要求运行,但是现在我希望有多个下拉菜单,以便可以选择多个帐户.我不希望多个选择位于同一下拉列表中.
and it currently works how I want it to but now I would like to have multiple drop down menus so I can select multiple accounts. I do not want the multiple select to be on the same dropdown.
如果我这样做:
<%= f.collection_select :account_ids, @accounts, :id, :name, include_blank: true %>
<%= f.collection_select :account_ids, @accounts, :id, :name, include_blank: true %>
<%= f.collection_select :account_ids, @accounts, :id, :name, include_blank: true %>
仅最后一个选择出现在参数中.我怎样才能使参数看起来像这样:
only the last selection appears in the params. How can I make it so the params would look like this:
"journal"=>{"account_ids"=>["1","2","3"]}
collection.select可以执行此操作吗?还是应该使用其他方式?任何帮助将不胜感激.谢谢!
Can collection.select do this or should I be using something different? Any help would be greatly appreciated. Thanks!
推荐答案
您需要添加一个选项:multiple
:
<%= f.collection_select :account_ids, @accounts,
:id, :name, { include_blank: true },
{ multiple: true } %>
注意: :multiple
-如果设置为 true
时,该选择将允许多个选择.
Note: :multiple
- If set to true
the selection will allow multiple choices.
我写了一个小片段来测试它.我的代码:
I wrote a little snippet to test it. My code :
<%= form_for @track, url: fetch_path do |f| %>
<%= f.collection_select :label, @tracks, :id, :title, {include_blank: true}, {multiple: true} %>
<% end %>
这是页面:
或者,如果您确实要复制:
Or, if you really want to duplicate:
<% klass = f.object.class.model_name.param_key %>
<%= f.collection_select :account_ids, @accounts, :id, :name, { include_blank: true } , { name: "#{klass}[account_ids][]" } %>
将以上行写3次.
这篇关于Rails:多个下拉菜单collection_select的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!