ruby-on-rails - rails : Multiple dropdown menus collection_select

标签 ruby-on-rails drop-down-menu collection-select

此处的 Super Rails n00b:目前我有一个包含以下代码的表单:

<%= 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 %>
<%= f.collection_select :account_ids, @accounts, :id, :name, include_blank: true %>

参数中仅显示最后一个选择。我怎样才能使参数看起来像这样:

"journal"=>{"account_ids"=>["1","2","3"]}

collection.select 可以做到这一点还是我应该使用不同的东西?任何帮助将不胜感激。谢谢!

最佳答案

您需要添加一个选项 :multiple :

<%= f.collection_select :account_ids, @accounts, 
                        :id, :name, { include_blank: true },
                         { multiple: true } %>

注::multiple - 如果设置为true,则选择将允许多个选择。

我写了一个小片段来测试它。我的代码:

<%= form_for @track, url: fetch_path do |f| %>
  <%= f.collection_select :label, @tracks, :id, :title, {include_blank: true}, {multiple: true} %>
<% end %>

这是页面:

drodown

或者,如果您确实想复制:

<% klass = f.object.class.model_name.param_key %>
<%= f.collection_select :account_ids, @accounts, :id, :name, { include_blank: true } , { name: "#{klass}[account_ids][]" } %>

将上面的行写 3 遍。

关于ruby-on-rails - rails : Multiple dropdown menus collection_select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30533381/

相关文章:

javascript - 使用 Bluefieldscom intl-tel-input,如何使标志下拉菜单不与其他输入重叠?

html - 向 Rails 中的 simple_form 提交按钮添加图标

css - 框大小或媒体查询创建空白?

ruby-on-rails - 自动测试时的 rspec 堆栈跟踪

jquery - Bootstrap 下拉按钮名称在单击时发生变化

javascript - jquery SlideToggle 使用此选择器

ruby-on-rails - 将 ActiveRecord 语句转换为 postgresql 查询 - Rails 4/postgresql 9.4

ruby - 在 Rails 4 中使用图像作为单选按钮标签

mysql - 使用 collection_select 按 user.id 过滤表单下拉列表

ruby-on-rails - rails 5 : How to pass collection_select values through strong_params in a fields_for?