我有一个 has_many 关联的多选框。参数如下:
foo_ids: ["1", "2", "3"]
使用强参数,我不允许这个属性,因为我想自己授权它,所以人们不能随便放任何他们想要的东西。
def update
bar.foos = authorized_foos
bar.update(baz_params)
respond_with bar
end
private
def authorized_foos
foos = Foo.find(params[:baz][:foo_ids])
foos.each do |foo|
authorize foo, :manage?
end
end
这种方法将迫使我找到所有的 foo,遍历它们,并单独授权每个。有没有更简单的方法来管理 has_many 授权,最好是使用 Pundit gem?