Ruby on rails Rails用于从一个窗体创建多个对象的强参数
我试图从一个窗体创建同一模型的多个对象。我在Ruby on rails Rails用于从一个窗体创建多个对象的强参数,ruby-on-rails,ruby-on-rails-4,ruby-on-rails-5,Ruby On Rails,Ruby On Rails 4,Ruby On Rails 5,我试图从一个窗体创建同一模型的多个对象。我在create方法中得到的参数如下所示: <ActionController::Parameters {"objects"=> <ActionController::Parameters { "0"=>{priority"=>"24", "style"=>"three_pictures"}, "1"=>{"priority"=>"24", "style"=>"three_pictures"}
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>
def create
params[:objects].each do |index, object|
Object.create(object.permit(:priority, :style))
end
...
end
这是可行的,但看起来不是正确的方法。这应该怎么做?我想我需要更多的信息。当我一次创建多个记录时,它通常是另一个对象的一部分,而我的记录是这样的
# 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
我希望这会有所帮助
Happy Hack:)如果你不想绕过大规模任务安全,我认为这是正确的做法。