I'm sure I have a basic rescue_from
question here but, I am trying to get a desired output when an exception is raised when my Validation
fails but I can't seem to figure it out.
Currently my ApplicationController
is as follows:
class ApplicationController < ActionController::API
include ActionController::Serialization
rescue_from ActiveRecord::RecordInvalid do |e|
render json: {error: e.message}, status: 404
end
end
and the JSON output I get is:
{
"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"
}
The desired ouput I would like to get (or something similar) is as follows:
{
"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"
]
}
I am not sure how to get this. I get the desired output when I comment out the rescue_from
method in the ApplicationController
and set the my RecordController
create method like this:
def create
r = Record.create(record_params)
r.save
if r.save
render json: r
else
render json: r.errors
end
end
Although this is what I want, I would have to go to every controller and add this but this would not be a DRY method... I would rather centralize this in the ApplicationController
Any help is appreciated. Thanks!
I have checked Correct JSON format & How can I “Pretty” format my JSON output in Ruby on Rails?