我的 Rails 项目中有一个名为“活动”的控制器,其中包含此模块“可排序”:
控制器:
class ActivitiesController < ApplicationController
include Sortable
...
模块(工作):
module Sortable
def save_positions
@ids = params[:ids]
# ~ Converting to array separating by ','
@ids = @ids.split(",")
count = 0
# ~ Saving new positions
for i in @ids
Activity.update(i, {:position => count})
count += 1
end
render :json => :success
end
end
但是当我重构代码时,我可以与其他控制器一起使用:
已编辑
require 'active_support/concern'
module Sortable
extend ActiveSupport::Concern
module InstanceMethods
def save_positions
@ids = params[:ids]
# ~ Converting to array separating by ','
@ids = @ids.split(",")
count = 0
# ~ Saving new positions
for i in @ids
update(i, {:position => count})
count += 1
end
render :json => :success
end
end
end
我做错了什么?
新错误消息:
ArgumentError (wrong number of arguments (2 for 0)): app/controllers/activities_controller.rb:61:in
我从以下位置删除了“活动”:
Activity.update(i, {:position => count})