Skip to content

Instantly share code, notes, and snippets.

@amoslanka
Created October 25, 2012 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amoslanka/3955573 to your computer and use it in GitHub Desktop.
Save amoslanka/3955573 to your computer and use it in GitHub Desktop.
ActiveResource 422 responses must send errors as full_messages
# If sent as normal errors:
# Response: {"errors": { "email": ["Email address is invalid","Email address can't be blank"]} }
>> NewsletterSubscriber.new(:email_address => "").save
ArgumentError: wrong number of arguments(2 for 1)
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:19:in `[]'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:19:in `block (2 levels) in from_array'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:18:in `each'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:18:in `detect'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:18:in `block in from_array'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:17:in `each'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:17:in `from_array'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:31:in `from_json'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:101:in `load_remote_errors'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:89:in `rescue in save_with_validation'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/validations.rb:72:in `save_with_validation'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/activeresource-3.2.8/lib/active_resource/observing.rb:19:in `save_with_notifications'
from (irb):31
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /Users/amoslanka/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
# Wierd error, particularly because the ActiveResource executes the POST, receives in response body the errors as json, and fails to parse them. The wierdness is that this isn't a clear process or step-by-step.
# If sent as full_messages
# Response: {"errors":["Email address is invalid","Email address can't be blank"]}
>> NewsletterSubscriber.create(:email_address => "")
=> #<NewsletterSubscriber:0x007fc0b4d0a800 @attributes={"email_address"=>""}, @remote_errors=#<ActiveResource::ResourceInvalid: Failed. Response code = 422. Response message = .>, @validation_context=nil, @errors=#<ActiveResource::Errors:0x007fc0b4d0a508 @base=#<NewsletterSubscriber:0x007fc0b4d0a800 ...>, @messages={:email_address=>["is invalid", "can't be blank"]}>>
# In other words, success.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment