-
-
Save amoslanka/3955573 to your computer and use it in GitHub Desktop.
ActiveResource 422 responses must send errors as full_messages
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
# 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. |
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
# 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