I have Server working with active models (not active record), and client side with activeresource .
For example server side controller have such code
...
def update
if @supplier.update_attributes(params[:supplier])
respond_to do |format|
format.any { head :ok }
end
else
respond_with(@supplier.errors,:status => :unprocessable_entity)
end
end
...
Active model has next code
...
class Subscriber < BaseModel
include ActiveModel::Validations
include ActiveModel::Serialization
include ActiveModel::Callbacks
extend ActiveModel::Naming
validates :email, :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
def update_attributes(attributes={})
self.attributes.merge!(attributes)
self.save
end
def save
if self.valid?
super
else
false
end
end
...
I type next code from rails console of client application
s = Supplier.find(13)
s.email = ''
s.save
return false because of invalid email
this is errors object from server before sending with respond_with
#<ActiveModel::Errors:0x000001012e9d78 @base=#<Supplier:0x000001032bf8c8 @attributes={:id=>13, :company_name=>.......}, @validation_context=nil, @errors=#<ActiveModel::Errors:0x000001012e9d78 ...>>, @messages={:email=>["is invalid"]}>
and client has next values of s
.... @remote_errors=#<ActiveResource::ResourceInvalid: Failed. Response code = 422. Response message = .>, @validation_context=nil, @errors=#<ActiveResource::Errors:0x000001034991d0 @base=#<Supplier:0x000001034af048 ...>, @messages={}>>
Why I can't take errors in client side (messages is empty hash)?
UPD: client get {} in body of response
"--- !ruby/object:Net::HTTPClientError \nbody: \"{}\"\nbody_exist: true\ncode: \"422\"\nheader: \n content-type: \n - application/json; charset=utf-8\n x-ua-compatible: \n - IE=Edge\n cache-control: \n - no-cache\n x-runtime: \n - \"0.128106\"\n content-length: \n - \"2\"\n server: \n - WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)\n date: \n - Thu, 29 Dec 2011 10:57:50 GMT\n connection: \n - close\n set-cookie: \n - _api_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJTM5OWQwNTZhMzI1MzcxYzdjZGI2NzNkZWZjNWE0OTQwBjsAVA%3D%3D--b9a0f65001dd994f269800f1c56259523ab669b1; path=/; HttpOnly\nhttp_version: \"1.1\"\nmessage: \"\"\nread: true\nsocket: \n"
UPD2 if controller action looks next
@supplier.update_attributes(params[:supplier])
respond_with(@supplier)
than response looks next
"--- !ruby/object:Net::HTTPClientError \nbody: \"{\\\"email\\\":[\\\"is invalid\\\"]}\"\nbody_exist: true\ncode: \"422\"\nheader: \n...
so I think problem is that client and server use different logic for serialization
class Errors < ActiveModel::Errors
# Grabs errors from a json response.
def from_json(json, save_cache = false)
array = Array.wrap(ActiveSupport::JSON.decode(json)['errors']) rescue []
from_array array, save_cache
end
client expects server uses errors key for errors hash ({:errors=>{:email=>["is invalid"]}}) but server doesn't!!(just {:email=>["is invalid"]})
What I miss ???
So I found problem myself. This is a bug of rails 3.1 controller responder It fixed in rails 3.2.0.rc1
pull request https://github.com/rails/rails/pull/3272/files