서버가 활성 모델 (활성 레코드가 아닌)로 작동하고 클라이언트 측이 activeresource로 작동합니다.레일 3.1.3 REST : 활성 리소스 쪽에서 오류를 검색 할 수 없습니다. (json 시리얼 라이저)
예를 들어서버 측 컨트롤러는 이러한 코드를 가지고
...
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
...
활성 모델은 다음 코드를 가지고
...
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
...
나는 클라이언트 응용 프로그램 거짓
s = Supplier.find(13)
s.email = ''
s.save
반환 레일 콘솔에서 다음 코드를 입력 이메일이 잘못되어
이 오류입니다가 respond_with와
#<ActiveModel::Errors:0x000001012e9d78 @base=#<Supplier:0x000001032bf8c8 @attributes={:id=>13, :company_name=>.......}, @validation_context=nil, @errors=#<ActiveModel::Errors:0x000001012e9d78 ...>>, @messages={:email=>["is invalid"]}>
를 전송하기 전에 서버에서 개체를 나는 클라이언트 측에서 오류를 취할 수없는 이유 클라이언트의
.... @remote_errors=#<ActiveResource::ResourceInvalid: Failed. Response code = 422. Response message = .>, @validation_context=nil, @errors=#<ActiveResource::Errors:0x000001034991d0 @base=#<Didww::Supplier:0x000001034af048 ...>, @messages={}>>
의 다음 값이 (메시지는 빈 해시) ?
UPD : 응답의 몸에 클라이언트 GET는 {}
"--- !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 컨트롤러 액션 응답에 비해
@supplier.update_attributes(params[:supplier])
respond_with(@supplier)
다음 그래서 다음
"--- !ruby/object:Net::HTTPClientError \nbody: \"{\\\"email\\\":[\\\"is invalid\\\"]}\"\nbody_exist: true\ncode: \"422\"\nheader: \n...
보이는 보이는 경우 나 잉크 문제가
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
클라이언트가 서버에서 오류가 해시에 대한 오류 키를 사용하여 기대 클라이언트와 서버가 직렬화에 대해 서로 다른 논리를 사용한다는 것입니다 ({: 오류 => {: 이메일 =>} [ "무효"]})하지만 서버 does not !! (just {: email => [ "is invalid"]})
나는 무엇이 놓친다?