2015-11-14 49 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?" 
    } 
}