Rails 从一个表单创建多个对象的强大参数

Rails strong params for creating multiple objects from one form

我正在尝试从一种形式创建同一模型的多个对象。我在 create 方法中得到的参数如下所示:

<ActionController::Parameters {"objects"=> <ActionController::Parameters {
  "0"=>{priority"=>"24", "style"=>"three_pictures"}, 
  "1"=>{"priority"=>"24", "style"=>"three_pictures"}, 
  "2"=>{"priority"=>"24", "style"=>"three_pictures"}
} permitted: false>}permitted: false>

我对在这种情况下使用 strong params 感到困惑。我的 create 方法如下所示:

def create
  params[:objects].each do |index, object|
    Object.create(object.permit(:priority, :style))
  end
  ...
end

这可行,但看起来不是正确的方法。应该怎么做?

我想我需要更多信息。当我一次创建超过 1 条记录时,它通常是另一个对象的子对象,我的看起来像这样

# Never trust parameters from the scary internet, only allow the white list through.
def family_params
  params.require(:family).permit(:name, users_attributes: [ :name, :email ])
end

希望对您有所帮助。

快乐黑客:)