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

fix: nested attributes with number as hash key now work #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/action_controller/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def each_element(value)
end

def fields_for_style?(object)
object.is_a?(Hash) && object.all? { |k, v| k =~ /\A-?\d+\z/ && v.is_a?(Hash) }
object.is_a?(Hash) && object.all? { |k, v| k.to_s =~ /\A-?\d+\z/ && v.is_a?(Hash) }
end

def unpermitted_parameters!(params)
Expand Down
17 changes: 17 additions & 0 deletions test/parameters_permit_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,23 @@ def assert_filtered_out(params, key)
assert_filtered_out permitted[:book][:authors_attributes]['0'], :age_of_death
end

test "fields_for_style_nested_params when key is number" do
params = ActionController::Parameters.new({
:book => {
:authors_attributes => {
0 => { :name => 'William Shakespeare', :age_of_death => '52' }
}
}
})
permitted = params.permit :book => { :authors_attributes => [ :name ] }

assert_not_nil permitted[:book][:authors_attributes][0]
assert_equal 'William Shakespeare', permitted[:book][:authors_attributes][0][:name]

assert_filtered_out permitted[:book][:authors_attributes][0], :age_of_death
end


test "fields_for_style_nested_params with negative numbers" do
params = ActionController::Parameters.new({
:book => {
Expand Down