Created
March 29, 2012 23:13
-
-
Save corny/2244799 to your computer and use it in GitHub Desktop.
Monkey patch to fix JSON error decoding in ActiveResource 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Monkey patch to fix Rails JSON error decoding | |
# https://gist.github.com/2244799 | |
class ActiveResource::Errors | |
def from_json(json, save_cache = false) | |
clear unless save_cache | |
decoded = ActiveSupport::JSON.decode(json) | |
if decoded.is_a?(Hash) | |
# Hash from Rails 3 | |
errors = decoded['errors'] | |
else | |
# Array from Rails 2 | |
errors = {} | |
for attr, value in decoded | |
(errors[attr] ||= []) << value | |
end | |
end | |
for attr, list in errors | |
list.each{|error| add(attr, error) } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment