提交 b5d4dd47 编写于 作者: T Thomas Walpole

Ensure that 'ActionController::Parameters' can still be passed to AR for collection associations

上级 2a0a264b
......@@ -445,6 +445,9 @@ def assign_nested_attributes_for_one_to_one_association(association_name, attrib
# ])
def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
options = self.nested_attributes_options[association_name]
if attributes_collection.respond_to?(:permitted?)
attributes_collection = attributes_collection.to_h
end
unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
raise ArgumentError, "Hash or Array expected, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
......@@ -471,6 +474,9 @@ def assign_nested_attributes_for_collection_association(association_name, attrib
end
attributes_collection.each do |attributes|
if attributes.respond_to?(:permitted?)
attributes = attributes.to_h
end
attributes = attributes.with_indifferent_access
if attributes['id'].blank?
......
......@@ -1074,12 +1074,16 @@ def permitted?
true
end
def [](key)
@hash[key]
end
def to_h
@hash
end
end
test "strong params style objects can be assigned" do
test "strong params style objects can be assigned for singular associations" do
params = { name: "Stern", ship_attributes:
ProtectedParameters.new(name: "The Black Rock") }
part = ShipPart.new(params)
......@@ -1087,4 +1091,12 @@ def to_h
assert_equal "Stern", part.name
assert_equal "The Black Rock", part.ship.name
end
test "strong params style objects can be assigned for collection associations" do
params = { trinkets_attributes: ProtectedParameters.new("0" => ProtectedParameters.new(name: "Necklace"), "1" => ProtectedParameters.new(name: "Spoon")) }
part = ShipPart.new(params)
assert_equal "Necklace", part.trinkets[0].name
assert_equal "Spoon", part.trinkets[1].name
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册