Last active
August 26, 2021 06:46
-
-
Save koenpunt/ac279e05cfeb0954ca763344fc0240b4 to your computer and use it in GitHub Desktop.
ActionController::Parameters -> permit_recursive_params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"tree"=> | |
{"name"=>"Australia", | |
"value"=>39904, | |
"id"=>6, | |
"description"=>"", | |
"string_array"=>["one", "two"], | |
"array_with_hashes"=>[{"a"=>"1"}, {"a"=>"2"}], | |
"type"=>{"name"=>"Other", "icon"=>"<i class=\"fa fa-pagelines\"></i>"}, | |
"children"=> | |
[{"name"=>"Boxes", | |
"value"=>451609, | |
"id"=>7, | |
"description"=>"Used to store things.", | |
"type"=>{"name"=>"Storage", "icon"=>"<i class=\"fa fa-archive\"></i>"}, | |
"children"=> | |
[{"name"=>"Boxes", | |
"value"=>451609, | |
"id"=>7, | |
"description"=>"Used to store things.", | |
"type"=> | |
{"name"=>"Storage", "icon"=>"<i class=\"fa fa-archive\"></i>"}, | |
"children"=> | |
[{"name"=>"Boxes", | |
"value"=>451609, | |
"id"=>7, | |
"description"=>"Used to store things.", | |
"type"=> | |
{"name"=>"Storage", | |
"icon"=>"<i class=\"fa fa-archive\"></i>"}}]}]}, | |
{"name"=>"Boxes", | |
"value"=>451609, | |
"id"=>7, | |
"description"=>"Used to store things.", | |
"type"=>{"name"=>"Storage", "icon"=>"<i class=\"fa fa-archive\"></i>"}}, | |
{"name"=>"Boxes", | |
"value"=>451609, | |
"id"=>7, | |
"description"=>"Used to store things.", | |
"type"=> | |
{"name"=>"Storage", "icon"=>"<i class=\"fa fa-archive\"></i>"}}]}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pp' | |
require 'active_support/core_ext/object/try' | |
require 'action_controller' | |
def permit_recursive_params(params) | |
(params.try(:to_unsafe_h) || params).map do |key, value| | |
if value.is_a?(Array) | |
if value.first.respond_to?(:map) | |
{ key => [ permit_recursive_params(value.first) ] } | |
else | |
{ key => [] } | |
end | |
elsif value.is_a?(Hash) | |
{ key => permit_recursive_params(value) } | |
else | |
key | |
end | |
end | |
end | |
params = ActionController::Parameters.new( | |
budget: { | |
tree: { | |
name: "Australia", | |
value: 39904, | |
id: 6, | |
description: "", | |
string_array: ["one", "two"], | |
array_with_hashes: [{ | |
a: "1" | |
}, { | |
a: "2" | |
}], | |
type: { | |
name: "Other", | |
icon: "<i class=\"fa fa-pagelines\"></i>" | |
}, | |
children: [ | |
{ | |
name: "Boxes", | |
value: 451609, | |
id: 7, | |
description: "Used to store things.", | |
type: { | |
name: "Storage", | |
icon: "<i class=\"fa fa-archive\"></i>" | |
}, | |
children: [ | |
{ | |
name: "Boxes", | |
value: 451609, | |
id: 7, | |
description: "Used to store things.", | |
type: { | |
name: "Storage", | |
icon: "<i class=\"fa fa-archive\"></i>" | |
}, | |
children: [ | |
{ | |
name: "Boxes", | |
value: 451609, | |
id: 7, | |
description: "Used to store things.", | |
type: { | |
name: "Storage", | |
icon: "<i class=\"fa fa-archive\"></i>" | |
} | |
} | |
] | |
} | |
] | |
}, | |
{ | |
name: "Boxes", | |
value: 451609, | |
id: 7, | |
description: "Used to store things.", | |
type: { | |
name: "Storage", | |
icon: "<i class=\"fa fa-archive\"></i>" | |
} | |
}, | |
{ | |
name: "Boxes", | |
value: 451609, | |
id: 7, | |
description: "Used to store things.", | |
type: { | |
name: "Storage", | |
icon: "<i class=\"fa fa-archive\"></i>" | |
} | |
} | |
] | |
} | |
} | |
) | |
pp params.require(:budget).permit(tree: permit_recursive_params(params[:budget][:tree])).to_h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't think this solution is working on every scenario:
For some reason, it does not whitelist the
field
node. I believe it's because you are whitelisting only the first node. In any case, I'm trying to fix it and I'll post it here when/if I finish it.