在rails中使用多选列表

Tan*_*ilo 4 ruby ruby-on-rails

我正在尝试在Rails中创建一个多选列表框.我的观看代码是:

<div>
  <%=nested_form_for(@allocation) do|builder|%>
    <%=builder.label :song_id, "Pick a song" %>

    <%=builder.select :song_id, options_for_select(
    Song.all.collect {|s| [ [s.title, s.artist].join(" by "), s.id ] }, 
    { include_blank: true, multiple: true, size: 5 }) %>

    <%=builder.submit "Add Song", class: "btn btn-large btn-primary" %>
  <% end %>
</div>
Run Code Online (Sandbox Code Playgroud)

目前我只有正常的单选择框,但我想将其转换为多选.任何指针都将非常感激.提前致谢

Tan*_*ilo 6

这似乎适用于我的情况:

<%= builder.select(
    :song_id,
    options_for_select(@selections),
    {},
    {multiple: true, size: 10})
%>
Run Code Online (Sandbox Code Playgroud)