I am using Rails 3. A Map has and belongs to many Collections, and vice-versa.
What Is already there …
I have a form that allows the user to select several maps that will go in a collection. For that, I use jQuery UI with the Selectable method in a grid.
This is how the grid is constructed:
<ol id="selectable">
<% for map in @user.maps %>
<li class="ui-state-default">
<strong><%= map.title %></strong><br>
<%= image_tag(map.thumbnail_url) %>
</li>
<% end %>
</ol>
It looks something like this:

When the user is finished selecting, the selected maps (their <li> tag) will have the class .ui-selected.
What I need …
How could I pass the controller the IDs of those selected maps in order to add them to a new collection? And what should I do in the controller? What are the best practices here?
What I thought about doing …
-
I’ve thought about writing a custom submit handler for the form, which would go through all of those
li.ui-selectedand somehow try to extract the IDs, then merge them into a hidden form field and finally submit the form. Then, in the controller, I’d split the hidden form field again, extract the IDs and get the Maps from the database. -
Another way would probably be to have a
collection_select, that somehow syncs with the jQuery Selectable. Thecollection_selectI have is:<%= collection_select :collection, :map_ids, @collection.user.maps, :id, :title, { :selected => @collection.map_ids }, { :class => "map_select", :multiple => true, :size => '10', :name => 'collection[map_ids][]' } %>
Both sound a bit hacky though. Isn’t there something easier or more JS/Rails-esque?
Here’s what I ended up with. This is my
collection_select.This is how my individual containers are created:
Here’s the Javascript that synchronizes them when the form is submitted: