最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Wrong number of arguments issue - Stack Overflow

matteradmin7PV0评论

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',
    plete: 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'

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',
    plete: 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'
Share Improve this question asked Dec 25, 2014 at 20:42 MuhambiMuhambi 3,5226 gold badges33 silver badges57 bronze badges 1
  • 1 Book.update_all({position: (index+1), id: id}) – adeneo Commented Dec 25, 2014 at 20:44
Add a ment  | 

2 Answers 2

Reset to default 5

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.

Post a comment

comment list (0)

  1. No comments so far