我正在尝试在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>
目前我只有正常的单一选择框,但我想将其转换为多选。任何指针都将非常感激。提前致谢
答案 0 :(得分:6)
这似乎适用于我的情况:
<%= builder.select(
:song_id,
options_for_select(@selections),
{},
{multiple: true, size: 10})
%>
答案 1 :(得分: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/
答案 2 :(得分:-1)
如果你想通过jquery做,以下链接将帮助你
http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/