4

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>

現時点では、通常の単一の選択ボックスがありますが、これを複数選択に変換したいと思います。任意のポインタをいただければ幸いです。前もって感謝します

4

3 に答える 3

6

これは私の場合はうまくいったようです:

<%= builder.select(
    :song_id,
    options_for_select(@selections),
    {},
    {multiple: true, size: 10})
%>
于 2012-07-12T11:33:59.793 に答える
4

多くの場合、select_tagを使用する必要がありますが、データを取得する場所に応じて、これが機能するさまざまな方法があります。

<%= select_tag '@Mymodel[myattribute][]',
    options_from_collection_for_select(SelectionModel, "id", "title", @Mymodel.myattribute),
    :multiple => true, :size =>10 }
%>

おそらくあなたは次のようになります

<%= select_tag '@allocation[song_id][]',
    options_from_collection_for_select(Song.all., "id", "title", @allocation.song_id),
    { :multiple => true, :size =>10 }
%>

この例は、ここで見ることができます...

http://www.gilluminate.com/2007/02/15/best-way-to-do-multiple-select-combo-boxes-in-rails/

于 2012-07-12T11:32:04.663 に答える
-1

jqueryで実行したい場合は、次のリンクが役立ちます

http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/

于 2012-07-12T11:21:17.190 に答える