Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6629771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:18:28+00:00 2026-05-25T22:18:28+00:00

I am using Rails 3. A Map has and belongs to many Collection s,

  • 0

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:

Grid example

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 …

  1. I’ve thought about writing a custom submit handler for the form, which would go through all of those li.ui-selected and 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.

  2. Another way would probably be to have a collection_select, that somehow syncs with the jQuery Selectable. The collection_select I 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:18:28+00:00Added an answer on May 25, 2026 at 10:18 pm

    Here’s what I ended up with. This is my collection_select.

    <%= collection_select :collection, 
        :map_ids, 
        @collection.user.maps, 
        :id,
        :title, 
        { :selected => nil },
        { :class => "map_select hiddenElement", :multiple => true, :size => '10', :name => 'collection[map_ids][]' }
    

    This is how my individual containers are created:

    <ol id="selectable">
      <% for map in current_user.maps %>
        <% if @collection.maps.include? map %>
          <li class="singleMapContainer ui-selected">
        <% else %>
          <li class="singleMapContainer">
        <% end %>
          <div class="singleMapInfo">
            <span class="mapId" style="display:none;"><%= map.id %></span>
            <strong><%= map.title %></strong><br>
            <%= map.created_at %><br>
            <%= image_tag(map.thumbnail_url) %>
          </div>
        </li>
      <% end %>
    </ol>
    

    Here’s the Javascript that synchronizes them when the form is submitted:

    $("#new_collection, .edit_collection").submit(function(){
      // iterate over each selected map container element
      $("li.ui-selected").each(function(){
        var mapId = $(this).find("span.mapId").html();  // find the map ID
        $("#collection_map_ids option[value='" + mapId + "']:first")[0].selected = true;  // synchronize the multiple select box
      });
      return true;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Rails 3.0.8 Lets say that I have two collections of users.
Using Rails 3.1.3 with Ruby 1.9.3p0. I've discovered that by default, Rails does not
I'm using rails 2.3.5 with i18n. I's there a way to find all not
I'm using Rails 2. I have resources nested like this: - university_categories - universities
I have two entities, projects and users. These are modeled in Rails using Mongoid
I have a Rails 3.1 app, running on Ruby 1.9.2, Mongo 2.0.2, using Mongoid
I have the following issue. I have a google map (using YM4r + Geokit)
I am using a few persisted Tire models in my Rails app. Searching has
I'm using rails 3 and I have two models, venues and areas where each
In the rails 2 version of my app I have this resource defined map.resources

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.