Skip to content

Instantly share code, notes, and snippets.

@corny
Created March 29, 2012 23:13
Show Gist options
  • Save corny/2244799 to your computer and use it in GitHub Desktop.
Save corny/2244799 to your computer and use it in GitHub Desktop.
Monkey patch to fix JSON error decoding in ActiveResource 3
# 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