I have a problem using ajax with rails method update_attributes. In js.coffee file I'm sending an ajax to compute a controller method.This ajax is simple and looks like this:
$.ajax({
url: 'tasks/update_data',
type: 'POST',
data: { swap: $.cookie("swap") }
});
What I'm trying to do is save row swaps after refreshing page. And this is what my controller method supposed to do.When I debug this method, this is what happens: if I swap once everything works fine; when I swap twice and output values to console, its outputs something like this third<=>second, second<=> first,but it must do third<=>second, third<=>first. So I think it's some trouble with mechanics of how db is updating and I don't understand why it works in such way.Between that, in all times, when update_attributes is called, it returns true, so no fail updates of db. This is how controller method looks like:
def update_data
ids = params[:swap].split('&')
puts ids
puts "--------------------------------------------------"
row1 = Task.where( id: ids[0] ).first
row2 = Task.where( id: ids[1] ).first
puts "first: " + row1.name
puts "second: " + row2.name
temp_hash1 = { name: row1.name, status: row1.status, project_id: row1.project_id, dead_line: row1.dead_line }
temp_hash2 = { name: row2.name, status: row2.status, project_id: row2.project_id, dead_line: row2.dead_line }
k = row1.update_attributes( temp_hash2 )
puts "first: " + row1.name
puts k
m =row2.update_attributes( temp_hash1 )
puts "second: " + row2.name
puts m
render nothing: true
end
Cookie value:
$.cookie( "swap", $.cookie("swap") + row_id + "&" + prev_row_id, { path: '/' } ).
Thanks for answering.