[英]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.