提交 a9b666b5 编写于 作者: K Krekoten' Marjan 提交者: José Valim

Fix generation of wrong json string when field has multiple errors

上级 76a15dd0
......@@ -168,6 +168,16 @@ def as_json(options=nil)
self
end
def encode_json(encoder)
errors = []
each_pair do |key, value|
value = value.first if value.size == 1
errors << "#{encoder.encode(key.to_s)}:#{encoder.encode(value, false)}"
end
"{#{errors * ','}}"
end
# Adds +message+ to the error messages on +attribute+, which will be returned on a call to
# <tt>on(attribute)</tt> for the same attribute. More than one error can be added to the same
# +attribute+ in which case an array will be returned on a call to <tt>on(attribute)</tt>.
......
......@@ -62,4 +62,14 @@ def self.lookup_ancestors
end
test 'to_json should return valid json string' do
person = Person.new
person.errors.add(:name, "can not be blank")
person.errors.add(:name, "can not be nil")
hash = ActiveSupport::OrderedHash[:name, ["can not be blank", "can not be nil"]]
assert_equal person.errors.to_json, hash.to_json
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册