2015-11-14 42 views
1

我敢肯定,我有一个基本的rescue_from问题在这里,但是,我试图得到所需的输出,当我的Validation失败时引发异常,但我似乎无法数字出来。如何渲染正确的JSON格式与提出的错误

目前我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" 
    ] 
} 

我不知道如何得到这个。我是说我注释掉ApplicationControllerrescue_from方法和设置我的RecordController创建方法是这样期望的输出:

def create 
    r = Record.create(record_params) 
    r.save 
    if r.save 
    render json: r 
    else 
     render json: r.errors 
    end 
    end 

虽然这是我想要的,我会去到每一个控制器和添加此但这不会是干的方法...我宁愿集中在ApplicationController

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

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

回答

-1

我在更多的研究后发现它。

在rescue_from身体异常对象erecord属性与导致异常的记录,你可以使用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?" 
    } 
} 

我希望这个答案可以帮助别人!欢呼声

0

你可以这样来做:

def create 
    r = Record.create(record_params) 
    if r.save 
    render json: r.to_json 
    else 
    render json: errors: r.errors, status: 422 
    end 
end 

如果并返回错误,它会返回此:

{ 
    errors: 
    { 
     attr_1:["can't be blank"], 
     attr_2:["can't be blank"] 
    } 
} 

与第一例如你显示,ruby的Exception#消息http://ruby-doc.org/core-2.2.0/Exception.html#method-i-message以你描述的方式返回错误。我通常看到rescue_from被用来返回通用的404页面。

希望这会有所帮助

+0

我试图防止自己不必添加代码,每个控制器和行动返回几个不同的键链接的规范。用你的方法,我必须这样做。这就是我只想向'ApplicationController'添加代码的原因。 – Awatatah

+2

没有尝试解析Exceptions消息,我不知道如何获得与对象本身返回的格式相同的格式。 –

0

只是一个更新,接受的答案不是JSON-API错误规范。

查看上面有可以与此错误对象

{ 
"error": { 
    "title": "RecordNotFound", 
    "detail": "Record not found for id: 44. Maybe deleted?" 
    } 
}