Rails 3.1.3 REST:无法从activeresource端检索错误(json序列化程序)

时间:2011-12-28 17:19:46

标签: ruby-on-rails ruby-on-rails-3 rest ruby-on-rails-3.1 activeresource

我让服务器使用活动模型(非活动记录),客户端使用activeresource。

例如,服务器端控制器具有此类代码

...
      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   
... 

我从客户端应用程序的rails控制台输入下一个代码

 s = Supplier.find(13)
 s.email = ''
 s.save

因电子邮件无效而返​​回false

这是来自服务器的错误对象,然后使用respond_with

发送
#<ActiveModel::Errors:0x000001012e9d78 @base=#<Supplier:0x000001032bf8c8 @attributes={:id=>13, :company_name=>.......}, @validation_context=nil, @errors=#<ActiveModel::Errors:0x000001012e9d78 ...>>, @messages={:email=>["is invalid"]}>

并且客户端具有s的下一个值

   .... @remote_errors=#<ActiveResource::ResourceInvalid: Failed.  Response code = 422.  Response message = .>, @validation_context=nil, @errors=#<ActiveResource::Errors:0x000001034991d0 @base=#<Didww::Supplier:0x000001034af048 ...>, @messages={}>> 

为什么我不能在客户端出错(消息是空哈希)?

UPD:客户端在响应正文中获取{}

"--- !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

客户端期望服务器使用错误键错误哈希({:errors =&gt; {:email =&gt; [“无效”]}}}但服务器没有!!(只是{:email =&gt; [ “无效”]})

我想念的是什么???

1 个答案:

答案 0 :(得分:1)

所以我自己发现了问题。 这是rails 3.1控制器响应器的错误 它固定在rails 3.2.0.rc1

拉请求 https://github.com/rails/rails/pull/3272/files

https://github.com/rails/rails/pull/3046/files