Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support fields_for attributes, which may have numeric symbols as hash…

… keys
  • Loading branch information...
commit 91bcebbdef0e31d38622785a064d023272f712db 1 parent 1aaf449
@guilleiguaran guilleiguaran authored
View
4 actionpack/lib/action_controller/metal/strong_parameters.rb
@@ -94,6 +94,10 @@ def convert_hashes_to_parameters(key, value)
def each_element(object)
if object.is_a?(Array)
object.map { |el| yield el }.compact
+ elsif object.is_a?(Hash) && object.keys.all? { |k| k =~ /\A-?\d+\z/ }
+ hash = object.class.new
+ object.each { |k,v| hash[k] = yield v }
+ hash
else
yield object
end
View
18 actionpack/test/controller/parameters/nested_parameters_test.rb
@@ -92,4 +92,22 @@ class NestedParametersTest < ActiveSupport::TestCase
permitted = params.permit book: { genre: :type }
assert_nil permitted[:book][:genre]
end
+
+ test "fields_for-style nested params" do
+ params = ActionController::Parameters.new({
+ book: {
+ authors_attributes: {
+ :'0' => { name: 'William Shakespeare', age_of_death: '52' },
+ :'-1' => { name: 'Unattributed Assistant' }
+ }
+ }
+ })
+ permitted = params.permit book: { authors_attributes: [ :name ] }
+
+ assert_not_nil permitted[:book][:authors_attributes]['0']
+ assert_not_nil permitted[:book][:authors_attributes]['-1']
+ assert_nil permitted[:book][:authors_attributes]['0'][:age_of_death]
+ assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['0'][:name]
+ assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['-1'][:name]
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.