簡體   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