From a4169f53f5acd23c7de45354a1d417c73d35a51b Mon Sep 17 00:00:00 2001 From: Salahutdinov Dmitry Date: Tue, 1 Dec 2015 14:09:09 +0500 Subject: [PATCH] fix: nested attributes with number as hash key now work --- lib/action_controller/parameters.rb | 2 +- test/parameters_permit_test.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/action_controller/parameters.rb b/lib/action_controller/parameters.rb index 9dc3d7d..81c5f07 100644 --- a/lib/action_controller/parameters.rb +++ b/lib/action_controller/parameters.rb @@ -222,7 +222,7 @@ def each_element(value) end def fields_for_style?(object) - object.is_a?(Hash) && object.all? { |k, v| k =~ /\A-?\d+\z/ && v.is_a?(Hash) } + object.is_a?(Hash) && object.all? { |k, v| k.to_s =~ /\A-?\d+\z/ && v.is_a?(Hash) } end def unpermitted_parameters!(params) diff --git a/test/parameters_permit_test.rb b/test/parameters_permit_test.rb index b1905a9..f04ca72 100644 --- a/test/parameters_permit_test.rb +++ b/test/parameters_permit_test.rb @@ -282,6 +282,23 @@ def assert_filtered_out(params, key) assert_filtered_out permitted[:book][:authors_attributes]['0'], :age_of_death end + test "fields_for_style_nested_params when key is number" do + params = ActionController::Parameters.new({ + :book => { + :authors_attributes => { + 0 => { :name => 'William Shakespeare', :age_of_death => '52' } + } + } + }) + permitted = params.permit :book => { :authors_attributes => [ :name ] } + + assert_not_nil permitted[:book][:authors_attributes][0] + assert_equal 'William Shakespeare', permitted[:book][:authors_attributes][0][:name] + + assert_filtered_out permitted[:book][:authors_attributes][0], :age_of_death + end + + test "fields_for_style_nested_params with negative numbers" do params = ActionController::Parameters.new({ :book => {