Module: FriendlyResponses

Defined in:
lib/friendly_responses.rb,
lib/friendly_responses/version.rb

Defined Under Namespace

Classes: ValidModelError

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#respond_with_invalid_record(model) ⇒ Object

Renders a friendly JSON error response


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/friendly_responses.rb', line 10

def respond_with_invalid_record(model)
  model_name = model.class.name.underscore.to_sym

  if model.valid?
    raise ValidModelError.new "can't render error response for valid model"
  end

  render status: :unprocessable_entity,
    json: {
      errors: model.errors.full_messages,
      model_name => { errors: model.errors }
    }
end