Updating MongoDB with AJAX

Question: I am using Jquery Sortable for a drag and drop feature on a list and can't figure out how to save the new order to the monogoDB database. I need to update the id, but mongoDB ID's are too long. How can i accomplish this?

Tech: ROR and MongoDB

Explanation: I have accomplished this with ROR and a Postgresql database. The issue comes from using MongoDB which does not allow me to use the gem 'Acts-as-list'... I tried to implement mongoid::orderable to establish a position but I am not familiar with mongoDB so I am not having luck...

I am trying to apply the same thing below to my MongoDB example, but the position is not getting updated to the DB.. I am using PUT instead of Patch for Mongo.

Working Example with ROR...

index.html (pardon my indentation)

    <script>
     $(function(){
     $('.section').sortable({
     update: function(event,ui){
     $.ajax({
     url: $(this).data("url"),
     type: "PATCH",
     data: $(this).sortable('serialize'),
     });
     }
     });
     });
    </script>

    <div>
      <div class="section" data-url="<%= sort_lists_path %>">
        <% @lists.each do |list| %>
        <%= link_to list.title, list, class: "lstm", id: dom_id(list) %>
        <% end %>
      </div>>
    </div>

Route

      resources :lists do
        collection do
            patch :sort
        end
      end

Controller Method

      def sort
        params[:list].each_with_index do |id, index|
          List.where(id: id).update_all(position: index + 1)
        end 
      head :ok
      end 
728x90

1 Answers Updating MongoDB with AJAX

Now sure who decided to KNOCK me without saying anything. That's great... -Anyhow, figured this out with my team. We changed the controller method to the following, and it is now updating the position in mongoDB...

Controller Method:

  def sort
    params[:status].each_with_index do |id, index|
    logger.debug ("HELLO: #{id} BELLO: #{index}")
      #Status.where(id: id).update_all(position: index + 1)
    s = Status.find(id)
    logger.debug("Checking my value #{s}")
    s.position = index+1
    s.save
      end 
   head :ok
  end 

4 months ago