Muhambi Muhambi - 6 months ago 7
Ruby Question

Wrong number of arguments issue

I have the following jQuery sortable code:

$( ".list" ).sortable({
update: function(){
$.ajax({
url: '/books/sort',
type: 'post',
items: 'li',
data: $('.list').sortable('serialize'),
dataType: 'script',
complete: function(request){
alert("jjjj");
}
});
}
});


and a sort action in my controller like:

def sort
params[:book].each_with_index do |id, index|
Book.update_all({position: index+1}, {id: id})
end
render nothing: true
end


but I get the error:

ArgumentError (wrong number of arguments (2 for 1)):
app/controllers/books_controller.rb:28:in `block in sort'

Answer

Do as below

Book.where(id: id).
  update_all(position: index+1)

If you read the documentation, it says explicitly :-

Parameters :

updates - A string, array, or hash representing the SET part of an SQL statement.