简体   繁体   English

jQuery AJAX POST到Rails Controller。 执行each_with_index时出错(TypeError无法将Symbol转换为Integer)

[英]jQuery AJAX POST to Rails Controller. Error when doing each_with_index (TypeError can't convert Symbol into Integer)

I'm trying to save the position of a jQuery-UI sortable connected list to a Rails backend. 我正在尝试将jQuery-UI可排序连接列表的位置保存到Rails后端。 I'm having trouble with the Rails Controller action when trying to save the parameters to database. 尝试将参数保存到数据库时,Rails Controller操作出现问题。 The error that I am getting is TypeError (can't convert Symbol into Integer): 我得到的错误是TypeError (can't convert Symbol into Integer):

The parameter from the Ajax POST request that I'm sending in looks like this which looks fine: 我正在发送的来自Ajax POST请求的参数看起来像这样:

Activity:[{"id":"65","column":"48"},{"id":"65","column":"48"},{"id":"67","column":"48"}]

My Rails controller action is this (I'm trying to update an Activity with an id of 'id' and updating the attributes position with 'position' and day_id with 'column': 我的Rails控制器操作是这样的(我正在尝试更新一个id为“ id”的Activity,并使用“ position”更新属性position和使用“ column”更新day_id:

  def sort
    JSON.parse(params[:Activity]).each_with_index do |x, index|
      id = x["id"]
      position = index+1
      column = x["column"]
      Activity.update_all(position = position, day_id = column, id = id)
    end
    render nothing: true
  end

I'm getting this error in Terminal: 我在终端中收到此错误:

    Started POST "/trips/sort" for 10.0.2.2 at 2012-11-04 23:52:30 +0000
Processing by TripsController#sort as */*
  Parameters: {"Activity"=>"[{\"id\":\"66\",\"column\":\"49\"},{\"id\":\"66\",\"column\":\"49\"},{\"id\":\"67\",\"column\":\"49\"}]"}
Completed 500 Internal Server Error in 1ms

TypeError (can't convert Symbol into Integer):

This is my jQuery AJAX call if helpful: 如果有帮助,这是我的jQuery AJAX调用:

    jQuery ->
  $('[id*="day"]').sortable(
    connectWith: ".day"
    placeholder: "ui-state-highlight"
    update: (event, ui) ->
      neworder = new Array()
      $(this).children().each ->
        column = $(this).parent().attr("id").match(/\d+/)[0]
        id = $(this).attr("id").match(/\d+/)[0]
        neworder.push(
          id: id
          column: column
        )
      alert neworder
      $.ajax
        url: "sort"
        type: "POST"
        data: { Activity: JSON.stringify(neworder) }
    ).disableSelection()

I've spent the past few hours on this and just can't figure it out. 我花了过去的几个小时来解决这个问题。 I really appreciate the time and help. 我非常感谢您的宝贵时间和帮助。

You want to update each activity individually, like so: 您想分别更新每个活动,如下所示:

JSON.parse(params[:Activity]).each_with_index do |x, index|
  id = x["id"]
  position = index+1
  column = x["column"]
  activity = Activity.find(id)
  activity.update_attributes(:position=>position, :column=>column)
end

(assuming your JSON parsing works correctly, which I haven't checked) (假设您的JSON解析正常,但我没有检查过)

Activity.update_all will update ALL of the activities with the same attributes (eg set all activities to column 5 and position 1), which is not what you want to accomplish here. Activity.update_all将使用相同的属性更新所有活动(例如,将所有活动设置为第5列和位置1),这不是您要在此处完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM