Formatting Parameters for Ajax POST request to Rails Controller - for jQuery-UI sortable list

Posted by Hung Luu on Stack Overflow See other posts from Stack Overflow or by Hung Luu
Published on 2012-11-03T23:15:42Z Indexed on 2012/11/05 5:00 UTC
Read the original article Hit count: 300

I'm using the jQuery-UI Sortable Connected Lists. I'm saving the order of the connected lists to a Rails server.

My approach is to grab the list ID, column ID and index position of each list item. I want to then wrap this into an object that can be passed as a parameter back to the Rails Controller to be saved into the database. So ideally i'm looking to format the parameter like this: Parameters: {"Activity"=>[{id:1,column:2,position:1},{id:2,column:2,position:2} ,...]}

How do I properly format my parameters to be passed in this Ajax POST request? Right now, with the approach below, I'm passing on Parameters: {"undefined"=>""}

This is my current jQuery code (Coffeescript) which doesn't work:

jQuery ->
  $('[id*="day"]').sortable(
    connectWith: ".day"
    placeholder: "ui-state-highlight"
    update: (event, ui) ->
      neworder = new Array()
      $('[id*="day"] > li').each ->
        column = $(this).attr("id")
        index = ui.item.index() + 1
        id = $("#" + column + " li:nth-child(" + index + ") ").attr('id')
        passObject={}
        passObject.id = id
        passObject.column = column
        passObject.index = index
        neworder.push(passObject)
      alert neworder
      $.ajax
        url: "sort"
        type: "POST"
        data: neworder
    ).disableSelection()

My apologies because this seems like a really amateur question but I'm just getting started with programming jQuery and Javascript.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about ruby-on-rails