繁体   English   中英

错误数量的论点

[英]Wrong number of arguments issue

我有以下jQuery可排序代码:

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

以及控制器中的排序操作,例如:

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

但是我得到了错误:

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

如果有人在这里想知道同样的事情,那么之所以会得到“错误数量的参数”,是因为Rails发生了变化(我相信是在4.0上),从而指定条件的部分移动了。

因此,要使其正常工作,您必须使用以下代码:

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

其他所有功能都将按预期工作。

做如下

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

如果您阅读该文档 ,则会明确指出:-

参数:

更新-表示SQL语句的SET部分的字符串,数组或哈希。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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