Skip to content

Instantly share code, notes, and snippets.

@nikz
Created June 8, 2009 10:49
Show Gist options
  • Select an option

  • Save nikz/125757 to your computer and use it in GitHub Desktop.

Select an option

Save nikz/125757 to your computer and use it in GitHub Desktop.
# BEFORE
>> a = User.new; a.valid?
>> a.errors.to_json
=> "[["name", "can't be blank"], ["password_confirmation", "can't be blank"], ["password", "can't be blank"], ["password", "is too short (minimum is 6 characters)"], ["email", "can't be blank"], ["email", "is too short (minimum is 6 characters)"], ["email", "should look like an email address."]]"
# config/initializers/sane_errors_to_json.rb
module ActiveRecord
class Errors
def to_json
@errors.to_json
end
end
end
# AFTER
>> a = User.new; a.valid?
>> a.errors.to_json
=> "{"name": ["can't be blank"], "password_confirmation": ["can't be blank"], "password": ["can't be blank", "is too short (minimum is 6 characters)"], "email": ["can't be blank", "is too short (minimum is 6 characters)", "should look like an email address."]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment