Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从web服务应用程序检索错误数据_Ruby On Rails_Ruby_Ruby On Rails 3_Response - Fatal编程技术网

Ruby on rails 从web服务应用程序检索错误数据

Ruby on rails 从web服务应用程序检索错误数据,ruby-on-rails,ruby,ruby-on-rails-3,response,Ruby On Rails,Ruby,Ruby On Rails 3,Response,我使用的是RubyonRails3,在发出HTTP POST请求后,我试图从web服务应用程序中检索错误数据。我想接收包括错误根目录在内的数据 在服务应用程序控制器中,我有 format.json { render :json => @account.errors, :status => 202 } 例如,返回的返回数据是 {\"base\":\"Invalid submitting\",\"name\":\"To short\"} 我想接收像这样的数据 # Note 'er

我使用的是RubyonRails3,在发出HTTP POST请求后,我试图从web服务应用程序中检索错误数据。我想接收包括
错误
根目录在内的数据

在服务应用程序控制器中,我有

format.json {
  render :json => @account.errors, :status => 202
}
例如,返回的返回数据是

{\"base\":\"Invalid submitting\",\"name\":\"To short\"}
我想接收像这样的数据

# Note 'errors'
"{\"errors\":{\"base\":\"Invalid submitting\",\"name\":\"To short\"}"}
我该怎么做呢?


一个解决办法就是这样做

render :json => '{"errors":' + @account.errors.to_json + '}'

但是我认为这不是正确的方法。RoR当然有一些功能可以更好地做到这一点…

您应该能够构造一个等效的哈希,然后使用它:

error_hash = { 'errors' => @account.errors.to_h }

render(:json => error_hash, :status => 302)

使用您的代码,我得到了这个错误“用户/accountsController中的StandardError#新的无效JSON字符串”。正确的版本是'error\u hash={'errors'=>@account.errors.to\u hash}'