Skip to content

Instantly share code, notes, and snippets.

@pascalesdedy
Created October 21, 2018 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pascalesdedy/b8b68701fc22633560f938961d03cd1a to your computer and use it in GitHub Desktop.
Save pascalesdedy/b8b68701fc22633560f938961d03cd1a to your computer and use it in GitHub Desktop.
app/controller/concerns files - Simple Rails API server - TDD
#app/controller/concerns/response.rb
module Response
def json_response(object, status = :ok)
render json: object, status: status
end
end
#app/controller/concerns/exception_handler.rb
module ExceptionHandler
# provides the more graceful `included` method
extend ActiveSupport::Concern
included do
rescue_from ActiveRecord::RecordNotFound do |e|
json_response({ message: e.message }, :not_found)
end
rescue_from ActiveRecord::RecordInvalid do |e|
json_response({ message: e.message }, :unprocessable_entity)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment