I am creating my first Web Service using Grape and I am confused by one thing. How do I respond with ActiveRecord validation errors when a POST request does not pass AR validation?
In my Foo model I have this:
validates :bar, {
presence: true,
uniqueness: true
}
My Foo Create API in Grape looks like this:
desc "Create a new Foo"
params do
requires :bar, type: String, allow_blank: false
end
post do
::Foo.create!({
bar: params[:bar]
})
end
As an example, when I create a Foo with Duplicate Bar, I am presented with a standard Rails Error page (Using Postman). How do I make sure that all my errors are only returned as JSON objects?
PS. I have the following instructions set up in my API class:
default_format :json
format :json
formatter :json, Grape::Formatter::ActiveModelSerializers