简体   繁体   English

错误数量的论点

[英]Wrong number of arguments issue

I have the following jQuery sortable code: 我有以下jQuery可排序代码:

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

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. 更新-表示SQL语句的SET部分的字符串,数组或哈希。

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

相关问题 参数数量错误(2为1),Ajax在rails中渲染 - wrong number of arguments (2 for 1), Ajax render in rails 在IE中出现错误,例如“参数数量错误或属性分配无效” - In IE getting error like “Wrong number of arguments or invalid property assignment” 从JavaScript运行WScript.Shell.Run()时获取“错误的参数数量或无效的属性赋值” - Getting “Wrong number of arguments or invalid property assignment” when running WScript.Shell.Run() from JavaScript 向Backbone.js集合添加参数会导致ArgumentError(错误的参数数量……用于过滤器方法) - Adding Parameters to Backbone.js collection causing ArgumentError (wrong number of arguments … for filter method 为什么我会收到销毁操作的ArgumentError(错误的参数数量(给定0,预期为1)),Rails 5.2 - Why am I receiving ArgumentError (wrong number of arguments (given 0, expected 1) for destroy action? Rails 5.2 setTimeout传递参数问题 - setTimeout passing arguments issue Typescript参数数量错误 - Typescript number of arguments error AngularJS tokenWrapper-错误的回调参数 - AngularJS tokenWrapper - wrong callback arguments 函数中可变数量的参数 - Variable number of arguments in a function Javascript未知数量的参数 - Javascript unknown number of arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM