Rails: Multiple dropdown menus collection_select

前端 未结 2 1372
谎友^
谎友^ 2021-01-26 23:47

Super Rails n00b here: Currently I have a form with the following code:

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


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-27 00:41

    You need to add one option :multiple :

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

    Here is the page :

    drodown

    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][]" } %>
    

    Write the above line 3 times.

提交回复
热议问题