Class: JsonApiServer::ValidationErrors
- Inherits:
-
Object
- Object
- JsonApiServer::ValidationErrors
- Includes:
- ApiVersion, Serializer
- Defined in:
- lib/json_api_server/validation_errors.rb
Overview
Converts ActiveModel validations to JSON API Errors. Spec: jsonapi.org/format/#error-objects.
Calling render_422(model_instance)
in a controller will create an instance of this class and render with appropriate headers.
i.e,
def create
topic = Topic.new(topic_params)
if topic.save
serializer = TopicSerializer.new(topic)
render json: serializer.to_json, status: :created
else
render_422(topic)
end
end
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(model) ⇒ ValidationErrors
constructor
A new instance of ValidationErrors.
Methods included from ApiVersion
Methods included from Serializer
Constructor Details
#initialize(model) ⇒ ValidationErrors
Returns a new instance of ValidationErrors.
24 25 26 27 |
# File 'lib/json_api_server/validation_errors.rb', line 24 def initialize(model) errors = get_errors(model) @errors = JsonApiServer::Errors.new(errors) end |
Instance Method Details
#as_json ⇒ Object
29 30 31 |
# File 'lib/json_api_server/validation_errors.rb', line 29 def as_json @errors.as_json end |