2016-10-26 59 views
1

我想从一种形式创建同一模型的多个对象。 PARAMS,我在我的create方法得到这个样子:Rails从一种形式创建多个对象的强参数

<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 

这工作,但看起来不像这样做的正确方法。这应该怎么做?

+0

我认为如果你不想绕过质量分配安全,它是正确的做法。 –

回答

0

我认为我需要更多信息。当我一次创建1个多记录其通常另一个对象的chield和我看起来是这样的

# 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 

我希望这会有所帮助。

快乐黑客:)

相关问题