简体   繁体   中英

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'

If someone lands here wondering the same thing, the reason why you get "Wrong number of arguments" it's cause there was a change in Rails (I believe it was on 4.0) that moved the section where you specify the condition.

So, for this to work, you'd have to use something like this:

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

And everything else will work as expected.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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