Skip to content

Instantly share code, notes, and snippets.

@arefaslani
Last active August 8, 2018 14:53
Show Gist options
  • Save arefaslani/81a5fe231e6c5b90627bcd6f23eb946f to your computer and use it in GitHub Desktop.
Save arefaslani/81a5fe231e6c5b90627bcd6f23eb946f to your computer and use it in GitHub Desktop.
JSON API Errors
{
"errors": [
{
"status": "400",
"source": { "pointer": "/data/attributes" },
"title": "parameter missing",
"detail": "parameter missed or empty: /data/attributes"
}
]
}
class UsersController < ApplicationController
def create
begin
render json: UserFactory.run(user_params), status: :created
rescue ActionController::ParameterMissing => e
return render json: {
errors: [
{
status: "422",
source: { pointer: e.param },
title: "parameter missing",
detail: "#{e.param} missed"
}
]
}, status: :unprocessable_entity
end
end
def user_params
params
.require(:data)
.require(:attributes)
.permit(:mobile, :email, :password)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment