gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:54:27 24 4
gpt4 key购买 nike

我确定我有一个基本的 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

我得到的 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"
}

我想获得(或类似的东西)所需的输出如下:
    {
"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"
]
}

我不知道如何得到这个。当我注释掉 rescue_from 时,我得到了想要的输出 ApplicationController 中的方法并设置我的 RecordController像这样创建方法:
  def create
r = Record.create(record_params)
r.save
if r.save
render json: r
else
render json: r.errors
end
end

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

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

我查了 Correct JSON format & How can I “Pretty” format my JSON output in Ruby on Rails?

最佳答案

经过更多的研究,我想通了。

异常对象 e在那个rescue_from body 里有一个record带有导致异常的记录的属性,您可以使用 errors该记录的属性以提取错误并使用您想要的格式创建响应:

我在 ApplicationController 中编辑了这一行到:

rescue_from ActiveRecord::RecordInvalid do |e|
render json: {error: {RecordInvalid: e.record.errors}}, status: 406
end

更改 RecordController创建方法:
def create
r = Record.create!(record_params)
render json: r
end

这将给出输出:
 {
"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"
]
}
}
}

如果您有其他异常,您想呈现示例,这真的很有帮助:
rescue_from ActiveRecord::RecordNotFound do
render json: {error: {RecordNotFound: "Record not found for id: #{params[:id]}. Maybe deleted?"}}, status: 404
end

如果我搜索无效记录 44它将呈现:
{
"error": {
"RecordNotFound": "Record not found for id: 44. Maybe deleted?"
}
}

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

关于ruby - 如何呈现正确的 JSON 格式并引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33704640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com