Ruby on rails 在rails中使用多选列表
我正在尝试在Rails中创建一个多选列表框。我的查看代码是:Ruby on rails 在rails中使用多选列表,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在尝试在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.art
<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>
目前我只有普通的单选框,但我想把它转换成多选框。任何指点都将不胜感激。提前感谢如果您想使用jquery,以下链接将帮助您
通常,您需要使用select_标记,但根据您从何处获取数据,这有很多不同的方法
<%= select_tag '@Mymodel[myattribute][]',
options_from_collection_for_select(SelectionModel, "id", "title", @Mymodel.myattribute),
:multiple => true, :size =>10 }
%>
true,:size=>10}
%>
也许你的看起来像
<%= select_tag '@allocation[song_id][]',
options_from_collection_for_select(Song.all., "id", "title", @allocation.song_id),
{ :multiple => true, :size =>10 }
%>
true,:size=>10}
%>
这里可以看到一个例子
这似乎在我的情况下起了作用:
<%= builder.select(
:song_id,
options_for_select(@selections),
{},
{multiple: true, size: 10})
%>
谢谢您,但目前我只想使用form\u来表示。我看过其他人的代码,但我不完全确定我的做法有什么不同……试着在include之前加上multiple:true\u blank:true。无论如何,这是正确的答案。不知道你是否可以使用SO的规则等等,但可能会更改它们(因为你同时使用了Q和A)?@TangoKilo数据库中的“song_id”是什么类型的?