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)
问题描述
我正在尝试将jQuery-UI可排序连接列表的位置保存到Rails后端.尝试将参数保存到数据库时,Rails Controller操作遇到麻烦.我收到的错误是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. I'm having trouble with the Rails Controller action when trying to save the parameters to database. The error that I am getting is TypeError (can't convert Symbol into Integer):
我要发送的Ajax POST请求中的参数看起来像这样:
The parameter from the Ajax POST request that I'm sending in looks like this which looks fine:
Activity:[{"id":"65","column":"48"},{"id":"65","column":"48"},{"id":"67","column":"48"}]
我的Rails控制器的操作是这样的(我正在尝试更新ID为"id"的Activity,并使用"position"更新属性position和使用"column"更新day_id:
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':
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):
如果有帮助,这是我的jQuery AJAX调用:
This is my jQuery AJAX call if helpful:
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
(假设您的JSON解析正常,但我没有检查)
(assuming your JSON parsing works correctly, which I haven't checked)
Activity.update_all
将使用相同的属性更新所有活动(例如,将所有活动设置为第5列和位置1),这不是您要在此处完成的.
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.
这篇关于jQuery AJAX POST到Rails Controller.执行each_with_index时出错(TypeError无法将Symbol转换为Integer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!