Using a list of multiple items in rails

I am trying to create a multi-user list in Rails. My view code:

<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> 

I currently have a regular single selectbox, but I want to convert it to multiselect. Any pointers would be much appreciated. thanks in advance

+4
source share
3 answers

This seems to have worked in my case:

 <%= builder.select( :song_id, options_for_select(@selections), {}, {multiple: true, size: 10}) %> 
+6
source

Often you need to use select_tag, but there are many different ways this can work, depending on where you get the data from

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

maybe yours looks something like this:

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

an example of this can be seen here ...

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

+4
source

If you want to do jquery, the following link will help you

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

0
source

All Articles