【问题标题】:jQuery Ajax call not working 100% in Rails appjQuery Ajax 调用在 Rails 应用程序中不能 100% 工作
【发布时间】:2015-05-14 13:51:25
【问题描述】:

Ajax 调用,如果一张卡被移到另一个堆栈,则会触发两次。有很多栈。如果卡片在同一个堆栈上发生变化,则调用只会触发一次。

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

【问题讨论】:

  • 你做了什么来调试这个?当它不起作用时,错误是什么样的?它是在控制器中失败还是控制器向客户端发送了意外响应?
  • 在发布问题后我似乎找到了答案,这真是令人惊讶。如果您仔细查看参数,您会看到 3 个唯一日期,而控制器仅识别 2 个。所以我将行从 dates.uniq.size == 2 更改为 dates.uniq.size > 1

标签: javascript jquery ruby-on-rails ajax json


【解决方案1】:

来自上面的评论。

有时我似乎在发布问题后就找到了答案。如果您仔细查看参数,您会看到 3 个唯一日期,而控制器仅识别 2 个。所以我将行从 dates.uniq.size == 2 更改为 dates.uniq.size > 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多