2015-05-14 54 views
0

Ajax調用,如果一張卡移動到另一個堆棧上,則觸發兩次。有很多堆棧。如果卡在同一個堆棧中更改,則只會觸發一次該呼叫。jQuery Ajax調用在Rails應用程序中不能100%工作

Ajax調用爲每個重新定位的記錄/卡發送一個id數組。然後控制器中的方法根據順序重新定位它們。

不知何故,這個過程似乎只對3/4調用起作用。

$(".row").sortable({ 
    connectWith: ".row", 
    handle: ".portlet-header", 
    cancel: ".portlet-toggle", 
    placeholder: "portlet-placeholder ui-corner-all", 
    update: function (event, ui) { 
     var data = { 
     'positions' : $(this).sortable('toArray', {attribute: "data-item"}), 
      'dates' : $(this).sortable('toArray', {attribute: "data-date"}), 
      'newdate' : $(this).closest('.row').attr("data-row-date") 
     } 

     // return ids in order 
      $.ajax({ 
      type: "PATCH", 
      async: true, 
      url: "/ticket_board/reposition_cards.json", 
      data: JSON.stringify(data), 
      dataType: 'json', 
      contentType: 'application/json' 
     }); 
     } 
    }); 

示例參數(從jQuery中排序的拖放卡片可排序)。

Started PATCH "/ticket_board/reposition_cards.json" for ::1 at 2015-05-14 08:25:38 -0500 
Processing by TicketBoardController#reposition_cards as JSON 
    Parameters: {"positions"=>["5", "8", "13", "1"], "dates"=>["2015-04-20", "2015-04-20", "2015-04-21", "2015-04-18"], "newdate"=>"2015-04-20", "ticket_board"=>{"positions"=>["5", "8", "13", "1"], "dates"=>["2015-04-20", "2015-04-20", "2015-04-21", "2015-04-18"], "newdate"=>"2015-04-20"}} 

控制器方法,其中ajax調用似乎約75%的時間工作。這對我的客戶至關重要。我真的需要幫助。

def reposition_cards 

    dates = params[:ticket_board][:dates] 
    positions = params[:ticket_board][:positions] 

    if dates or positions 

     # move job on the same stack 
     if dates.size > 1 and dates.uniq.size == 1 
     positions.each_with_index do |ticket_id, index| 
      Ticket.find(ticket_id).update(calendar_order: index) 
     end 

     # move job to empty stack 
     elsif dates.size == 1 
      positions.each_with_index do |ticket_id, index| 
      Ticket.find(ticket_id). 
       update(calendar_order: index, calendar_date: params[:newdate]) 
      end 

     # useless case 
     elsif dates.size == 0 
     nil 

     # add job to another existing stack 
     elsif dates.size > 1 and dates.uniq.size == 2 
     positions.each_with_index do |ticket_id, index| 
      Ticket.find(ticket_id). 
      update(calendar_order: index, 
      calendar_date: params[:newdate]) 
     end 
     end 
    end 
    respond_to do |format| 
     format.json { render json: @tickets, status: :ok } 
    end 
    end 
+0

你做了什麼來調試呢?什麼錯誤看起來不起作用?它在控制器中發生故障還是控制器向客戶端發送意外的響應? – pjmorse

+1

這是驚人的,我似乎在發佈問題後才找到答案。如果你仔細觀察參數,你會看到3個獨特的日期,而控制器只識別2個。所以我將行從'dates.uniq.size == 2'更改爲'dates.uniq.size> 1 ' – tidelake

回答

0

從上面的評論。

有時我似乎在發佈問題後才找到答案。如果你仔細觀察參數,你會看到3個獨特的日期,而控制器只識別2個。所以我把線從dates.uniq.size == 2更改爲dates.uniq.size > 1

相關問題