Skip to content

Commit 62b7ad4

Browse files
committed
Merge pull request #30965 from kmanzana/master
Wrap accepted nested attributes params
2 parents 08df10a + dd423a7 commit 62b7ad4

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

actionpack/lib/action_controller/metal/params_wrapper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ def include
112112
else
113113
self.include = m.attribute_names
114114
end
115+
116+
if m.respond_to?(:nested_attributes_options) && m.nested_attributes_options.keys.any?
117+
self.include += m.nested_attributes_options.keys.map do |key|
118+
key.to_s.concat("_attributes")
119+
end
120+
end
121+
122+
self.include
115123
end
116124
end
117125
end

actionpack/test/controller/params_wrapper_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,20 @@ def test_handles_empty_content_type
255255
assert_equal "", @response.body
256256
end
257257
end
258+
259+
def test_derived_wrapped_keys_from_nested_attributes
260+
def User.nested_attributes_options
261+
{ person: {} }
262+
end
263+
264+
assert_called(User, :attribute_names, times: 2, returns: ["username"]) do
265+
with_default_wrapper_options do
266+
@request.env["CONTENT_TYPE"] = "application/json"
267+
post :parse, params: { "username" => "sikachu", "person_attributes" => { "title" => "Developer" } }
268+
assert_parameters("username" => "sikachu", "person_attributes" => { "title" => "Developer" }, "user" => { "username" => "sikachu", "person_attributes" => { "title" => "Developer" } })
269+
end
270+
end
271+
end
258272
end
259273

260274
class NamespacedParamsWrapperTest < ActionController::TestCase

0 commit comments

Comments
 (0)