1

ここに 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 %>

最後の選択のみが params に表示されます。パラメータが次のようになるようにするにはどうすればよいですか。

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

collection.select はこれを行うことができますか、それとも別のものを使用する必要がありますか? どんな助けでも大歓迎です。ありがとう!

4

2 に答える 2

2

1 つのオプションを追加する必要があります: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 %>

ここにページがあります:

ドロップダウン

または、本当に複製したい場合:

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

上記の行を 3 回書きます。

于 2015-05-29T15:29:30.310 に答える
0

パラメータ名が「[]」で終わる場合、その名前を持つすべての入力は、その名前を持つ配列に照合されます。

したがって、select タグ (html 内) は次のようになります。

<select name="account_ids[]"><option>...

collection_selectヘルパーを使用してこれを作成するには、試してください

<%= f.collection_select :account_ids, @accounts, :id, :name, {include_blank: true}, {:name => 'account_ids[]'} %>
于 2015-05-29T15:31:04.280 に答える