app/controllers/application_controller.rb
# Renders a JSON error for any failed operation for any reason, as follows:
#   { error: 'Some error message' }
#
# The reason for failure could be anything:
# * An exception was thrown
# * The request did not have a required query parameter
# * A query parameter was badly formatted
#
# Supports localization of error messages.
# For example, if i18n_prefix is 'klass.method', and error is 'some_error',
# then it will look for this localized error message:
#   op_errors:
#     klass:
#       method:
#         some_error: Some error message
#
# @param i18n_prefix [String] usually 'class_name.method_name'. Can be nil
# @param error [String, Symbol] eg. :some_parameter_missing
# @param status [Symbol] the HTTP status code to be used for the response
Renders a JSON error for any failed operation for any reason, as follows:
{ error: 'Some error message' }

The reason for failure could be anything:
An exception was thrown
The request did not have a required query parameter
A query parameter was badly formatted

Supports localization of error messages.
Renders a JSON error for any failed operation for any reason as follows : { error : ' Some error message ' } The reason for failure could be anything : An exception was thrown The request did not have a required query parameter A query parameter was badly formatted Supports localization of error messages .
def render_op_error (i18n_prefix, error, status = :bad_request)
    respond_to do |format|
      format.json {
        i18n_key = 'op_errors.'
        i18n_key += "#{i18n_prefix}." if i18n_prefix.present?
        i18n_key += error.to_s
        render json: { error: I18n.t(i18n_key, default: error.to_s.humanize) },
               status: status
      }
    end
  end
def render_op_error ( i18n_prefix , error , status = :bad_request ) respond_to do | format | format . json { i18n_key = 'op_errors.' i18n_key += "#{i18n_prefix}." if i18n_prefix . present? i18n_key += error . to_s render json : { error : I18n . t ( i18n_key , default : error . to_s . humanize ) } , status : status } end end
Renders a JSON error for any failed operation for any reason, as follows:
{ error: 'Some error message' }
Renders a JSON error for any failed operation for any reason as follows : { error : ' Some error message ' }
i18n_prefix
usually 'class_name.method_name'. Can be nil
usually ' class_name . method_name ' . Can be nil
error
None
status
the HTTP status code to be used for the response
the HTTP status code to be used for the response