From 83482256efec140b53adde50ba8263af6af7a016 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Fri, 30 Nov 2012 11:24:16 -0500 Subject: [PATCH] hash filters should be accessed with symbols or strings --- .../metal/strong_parameters.rb | 8 +++--- .../parameters/nested_parameters_test.rb | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb index 99a4e312cf..386075bd30 100644 --- a/actionpack/lib/action_controller/metal/strong_parameters.rb +++ b/actionpack/lib/action_controller/metal/strong_parameters.rb @@ -124,10 +124,10 @@ def permit! # ActionController::ParameterMissing error. # # ActionController::Parameters.new(person: { name: 'Francesco' }).require(:person) - # # => {"name"=>"Francesco"} + # # => {"name"=>"Francesco"} # # ActionController::Parameters.new(person: nil).require(:person) - # # => ActionController::ParameterMissing: param not found: person + # # => ActionController::ParameterMissing: param not found: person # # ActionController::Parameters.new(person: {}).require(:person) # # => ActionController::ParameterMissing: param not found: person @@ -187,7 +187,7 @@ def require(key) # # => {} # # params.require(:person).permit(contact: :phone) - # # => {"contact"=>{"phone"=>"555-1234"}} + # # => {"contact"=>{"phone"=>"555-1234"}} # # params.require(:person).permit(contact: [ :email, :phone ]) # # => {"contact"=>{"email"=>"none@test.com", "phone"=>"555-1234"}} @@ -203,6 +203,8 @@ def permit(*filters) end keys.grep(/\A#{Regexp.escape(filter)}\(\d+[if]?\)\z/) { |key| params[key] = self[key] } when Hash then + filter = filter.with_indifferent_access + self.slice(*filter.keys).each do |key, values| return unless values diff --git a/actionpack/test/controller/parameters/nested_parameters_test.rb b/actionpack/test/controller/parameters/nested_parameters_test.rb index d287e79cba..6df849c4e2 100644 --- a/actionpack/test/controller/parameters/nested_parameters_test.rb +++ b/actionpack/test/controller/parameters/nested_parameters_test.rb @@ -36,6 +36,31 @@ class NestedParametersTest < ActiveSupport::TestCase assert_nil permitted[:magazine] end + test "permitted nested parameters with a string or a symbol as a key" do + params = ActionController::Parameters.new({ + book: { + 'authors' => [ + { name: 'William Shakespeare', born: '1564-04-26' }, + { name: 'Christopher Marlowe' } + ] + } + }) + + permitted = params.permit book: [ { 'authors' => [ :name ] } ] + + assert_equal 'William Shakespeare', permitted[:book]['authors'][0][:name] + assert_equal 'William Shakespeare', permitted[:book][:authors][0][:name] + assert_equal 'Christopher Marlowe', permitted[:book]['authors'][1][:name] + assert_equal 'Christopher Marlowe', permitted[:book][:authors][1][:name] + + permitted = params.permit book: [ { authors: [ :name ] } ] + + assert_equal 'William Shakespeare', permitted[:book]['authors'][0][:name] + assert_equal 'William Shakespeare', permitted[:book][:authors][0][:name] + assert_equal 'Christopher Marlowe', permitted[:book]['authors'][1][:name] + assert_equal 'Christopher Marlowe', permitted[:book][:authors][1][:name] + end + test "nested arrays with strings" do params = ActionController::Parameters.new({ :book => { -- GitLab