如何呈现正确的 JSON 格式并引发错误

Awa*_*tah 5 ruby json ruby-on-rails-4

我确定我rescue_from在这里有一个基本问题,但是,当我Validation失败时引发异常时,我试图获得所需的输出,但我似乎无法弄清楚。

目前我的ApplicationController情况如下:

class ApplicationController < ActionController::API
  include ActionController::Serialization

  rescue_from ActiveRecord::RecordInvalid do |e|
  render json: {error: e.message}, status: 404
  end  

end
Run Code Online (Sandbox Code Playgroud)

我得到的 JSON 输出是:

    {
  "error": "Validation failed: Organization can't be blank, Description can't be blank, Artwork can't be blank, Language code can't be blank, Copyright can't be blank, Right to left is not included in     the list, Digital audio is not included in the list, Portion is not included in the list, Electronic text is not included in the list, Old is not included in the list, New is not included in the list"
}
Run Code Online (Sandbox Code Playgroud)

我想获得(或类似的东西)所需的输出如下:

    {
  "organization_id": [
    "can't be blank"
  ],
  "description": [
    "can't be blank"
  ],
  "artwork": [
    "can't be blank"
  ],
  "language_code": [
    "can't be blank"
  ],
  "copyright": [
    "can't be blank"
  ],
  "right_to_left": [
    "is not included in the list"
  ],
  "digital_audio": [
    "is not included in the list"
  ],
  "portion": [
    "is not included in the list"
  ],
  "electronic_text": [
    "is not included in the list"
  ],
  "old": [
    "is not included in the list"
  ],
  "new": [
    "is not included in the list"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何得到这个。当我注释掉 中的rescue_from方法ApplicationControllerRecordController像这样设置我的create 方法时,我得到了所需的输出:

  def create
    r = Record.create(record_params)
    r.save
    if r.save
    render json: r
    else
      render json: r.errors
    end
  end
Run Code Online (Sandbox Code Playgroud)

虽然这是我想要的,但我必须去每个控制器并添加它,但这不是一个 DRY 方法......我宁愿将它集中在 ApplicationController

任何帮助表示赞赏。谢谢!

我已经检查了正确的 JSON 格式以及如何在 Ruby on Rails 中“漂亮”格式化我的 JSON 输出?

Awa*_*tah 4

经过更多研究后我发现了这一点。

e该rescue_from主体中的异常对象具有record包含导致异常的记录的属性,您可以使用errors该记录的属性来提取错误并创建具有所需格式的响应:

我将此行编辑ApplicationController为:

rescue_from ActiveRecord::RecordInvalid do |e|
  render json: {error: {RecordInvalid: e.record.errors}}, status: 406
end
Run Code Online (Sandbox Code Playgroud)

RecordController将创建方法更改为:

def create
    r = Record.create!(record_params)
    render json: r
end
Run Code Online (Sandbox Code Playgroud)

这将给出输出:

 {
  "error": {
    "RecordInvalid": {
      "organization_id": [
        "can't be blank"
      ],
      "name": [
        "can't be blank"
      ],
      "description": [
        "can't be blank"
      ],
      "artwork": [
        "can't be blank"
      ],
      "language_code": [
        "can't be blank"
      ],
      "copyright": [
        "can't be blank"
      ],
      "right_to_left": [
        "is not included in the list"
      ],
      "digital_audio": [
        "is not included in the list"
      ],
      "portion": [
        "is not included in the list"
      ],
      "electronic_text": [
        "is not included in the list"
      ],
      "old": [
        "is not included in the list"
      ],
      "new": [
        "is not included in the list"
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您遇到其他异常并希望呈现示例,这确实很有帮助:

rescue_from ActiveRecord::RecordNotFound do
  render json: {error: {RecordNotFound: "Record not found for id: #{params[:id]}. Maybe deleted?"}}, status: 404    
end 
Run Code Online (Sandbox Code Playgroud)

如果我搜索无效记录,44它将呈现:

{
 "error": {
    "RecordNotFound": "Record not found for id: 44. Maybe deleted?"
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望这个答案可以帮助其他人!干杯