@@ -194,6 +194,75 @@ def assert_filtered_out(params, key)
194
194
assert_filtered_out permitted [ :book ] [ :authors_attributes ] [ "-1" ] , :age_of_death
195
195
end
196
196
197
+ test "nested params with numeric keys addressing individual numeric keys" do
198
+ params = ActionController ::Parameters . new (
199
+ book : {
200
+ authors_attributes : {
201
+ '0' : { name : "William Shakespeare" , age_of_death : "52" } ,
202
+ '1' : { name : "Unattributed Assistant" } ,
203
+ '2' : { name : %w( injected names ) }
204
+ }
205
+ } )
206
+ permitted = params . permit book : { authors_attributes : { '1' : [ :name ] , '0' : [ :name , :age_of_death ] } }
207
+
208
+ assert_equal (
209
+ { "book" => { "authors_attributes" => { "0" => { "name" => "William Shakespeare" , "age_of_death" => "52" } , "1" => { "name" => "Unattributed Assistant" } } } } ,
210
+ permitted . to_h
211
+ )
212
+ end
213
+
214
+ test "nested params with numeric keys addressing individual numeric keys using require first" do
215
+ params = ActionController ::Parameters . new (
216
+ book : {
217
+ authors_attributes : {
218
+ '0' : { name : "William Shakespeare" , age_of_death : "52" } ,
219
+ '1' : { name : "Unattributed Assistant" } ,
220
+ '2' : { name : %w( injected names ) }
221
+ }
222
+ } )
223
+
224
+ permitted = params . require ( :book ) . permit ( authors_attributes : { '1' : [ :name ] } )
225
+
226
+ assert_equal (
227
+ { "authors_attributes" => { "1" => { "name" => "Unattributed Assistant" } } } ,
228
+ permitted . to_h
229
+ )
230
+ end
231
+
232
+ test "nested params with numeric keys addressing individual numeric keys to arrays" do
233
+ params = ActionController ::Parameters . new (
234
+ book : {
235
+ authors_attributes : {
236
+ '0' : [ "draft 1" , "draft 2" , "draft 3" ] ,
237
+ '1' : [ "final draft" ] ,
238
+ '2' : { name : %w( injected names ) }
239
+ }
240
+ } )
241
+ permitted = params . permit book : { authors_attributes : { '2' : [ :name ] , '0' : [ ] } }
242
+
243
+ assert_equal (
244
+ { "book" => { "authors_attributes" => { "2" => { } , "0" => [ "draft 1" , "draft 2" , "draft 3" ] } } } ,
245
+ permitted . to_h
246
+ )
247
+ end
248
+
249
+ test "nested params with numeric keys addressing individual numeric keys to more nested params" do
250
+ params = ActionController ::Parameters . new (
251
+ book : {
252
+ authors_attributes : {
253
+ '0' : [ "draft 1" , "draft 2" , "draft 3" ] ,
254
+ '1' : [ "final draft" ] ,
255
+ '2' : { name : { "projects" => [ "hamlet" , "Othello" ] } }
256
+ }
257
+ } )
258
+ permitted = params . permit book : { authors_attributes : { '2' : { name : { projects : [ ] } } , '0' : [ ] } }
259
+
260
+ assert_equal (
261
+ { "book" => { "authors_attributes" => { "2" => { "name" => { "projects" => [ "hamlet" , "Othello" ] } } , "0" => [ "draft 1" , "draft 2" , "draft 3" ] } } } ,
262
+ permitted . to_h
263
+ )
264
+ end
265
+
197
266
test "nested number as key" do
198
267
params = ActionController ::Parameters . new (
199
268
product : {
0 commit comments