1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

hash filters should be accessed with symbols or strings

This commit is contained in:
Francesco Rodriguez 2012-11-30 11:24:16 -05:00
parent 64c53d7ce4
commit 83482256ef
2 changed files with 30 additions and 3 deletions

View file

@ -124,10 +124,10 @@ module ActionController
# <tt>ActionController::ParameterMissing</tt> 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 @@ module ActionController
# # => {}
#
# 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 @@ module ActionController
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

View file

@ -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 => {