Skip to content
This repository has been archived by the owner on Aug 17, 2017. It is now read-only.

Commit

Permalink
Merge commit 'd3ea93c61844b248d60493e0d58e42f2327454c6'
Browse files Browse the repository at this point in the history
  • Loading branch information
fvaleur committed Jul 24, 2012
2 parents 60508bd + d3ea93c commit 3f3acef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/action_controller/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def permit!
def require(key)
self[key].presence || raise(ActionController::ParameterMissing.new(key))
end

alias :required :require

def permit(*filters)
Expand Down Expand Up @@ -97,6 +97,11 @@ def convert_hashes_to_parameters(key, value)
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/ }
hash = object.class.new
object.each { |k,v| hash[k] = yield v }
hash
else
yield object
end
Expand Down
18 changes: 18 additions & 0 deletions test/nested_parameters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,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

0 comments on commit 3f3acef

Please sign in to comment.