Permalink
Browse files

Merge pull request #30 from jetthoughts/master

Support fields_for attributes, which may have negative numeric symbols as hash keys
  • Loading branch information...
2 parents 8d33574 + b2bed9d commit ae94e127ee8f689f1cd58a2bbd925aee484db581 @rafaelfranca rafaelfranca committed Aug 17, 2012
Showing with 19 additions and 1 deletion.
  1. +1 −1 lib/action_controller/parameters.rb
  2. +18 −0 test/nested_parameters_test.rb
@@ -94,7 +94,7 @@ def each_element(object)
if object.is_a?(Array)
object.map { |el| yield el }.compact
# fields_for on an array of records uses numeric hash keys
- elsif object.is_a?(Hash) && object.keys.all? { |k| k =~ /\A\d+\z/ }
+ 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
@@ -110,4 +110,22 @@ class NestedParametersTest < ActiveSupport::TestCase
assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['0'][:name]
assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['1'][:name]
end
+
+ test "fields_for_style_nested_params with negative numbers" do
+ params = ActionController::Parameters.new({
+ book: {
+ authors_attributes: {
+ :'-1' => {name: 'William Shakespeare', age_of_death: '52'},
+ :'-2' => {name: 'Unattributed Assistant'}
+ }
+ }
+ })
+ permitted = params.permit book: {authors_attributes: [:name]}
+
+ assert_not_nil permitted[:book][:authors_attributes]['-1']
+ assert_not_nil permitted[:book][:authors_attributes]['-2']
+ assert_nil permitted[:book][:authors_attributes]['-1'][:age_of_death]
+ assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['-1'][:name]
+ assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['-2'][:name]
+ end
end

0 comments on commit ae94e12

Please sign in to comment.