Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

hash filters should be accessed with symbols or strings

  • Loading branch information...
commit d0062e7b1e6fd87b39aad4618c7c5f71ce252070 1 parent 3e363d7
Francesco Rodriguez authored
View
2  lib/action_controller/parameters.rb
@@ -46,6 +46,8 @@ def permit(*filters)
params[filter] = self[filter] if has_key?(filter)
keys.grep(/\A#{Regexp.escape(filter.to_s)}\(\d+[if]?\)\z/).each { |key| params[key] = self[key] }
when Hash then
+ filter = filter.with_indifferent_access
+
self.slice(*filter.keys).each do |key, value|
return unless value
View
27 test/nested_parameters_test.rb
@@ -32,6 +32,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 => {
@@ -112,7 +137,7 @@ class NestedParametersTest < ActiveSupport::TestCase
end
test "fields_for_style_nested_params with negative numbers" do
- params = ActionController::Parameters.new({
+ params = ActionController::Parameters.new({
:book => {
:authors_attributes => {
:'-1' => { :name => 'William Shakespeare', :age_of_death => '52' },
Please sign in to comment.
Something went wrong with that request. Please try again.