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):
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"}]
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):
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:
(assuming your JSON parsing works correctly, which I haven’t checked)
Activity.update_allwill 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.