admin管理员组

文章数量:1434929

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.

本文标签: javascriptWrong number of arguments issueStack Overflow