Answer the question
In order to leave comments, you need to log in
How to render a method with a status?
Hello!
There is a call_404 method
render :json => Error::call_404, status: :not_found
status: :not_found
Answer the question
In order to leave comments, you need to log in
We usually do something like this. Adding methods to the base controller
def respond_with_400(exception)
render json: { success: false, all_errors: ['The user has not authorized application'] }, status: 400
end
def respond_with_404(exception=nil)
render json: { success: false, all_errors: [t('errors.not_found')] }, status: 404
end
def respond_with_500(exception)
render json: {success: false, all_errors: [t('errors.something_went_wrong')], debug: exception.to_s}, status: 500
end
def respond_with_success(resource=nil, status=200)
result = {success: true}
result.merge!(:"#{resource.class.name.underscore}" => {id: resource.id}) if resource.present?
render json: result, status: status
end
def respond_with_errors(resource=nil, status=422, debug=nil)
result = {success: false}
result.merge!(errors: resource.errors.messages, all_errors: resource.errors.full_messages) if resource.present?
result.merge!(debug: debug) if debug.present?
render json: result, status: status
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question