view raw
Muhambi Muhambi - 11 months ago 25
Ruby Question

Wrong number of arguments issue

I have the following jQuery sortable code:

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

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})
render nothing: true

but I get the error:

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


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.