Para que la respuesta JSON en los errores, sólo tiene que añadir el siguiente código a su application_controller:
rescue_from Exception, :with => :render_error
rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
rescue_from ActionController::RoutingError, :with => :render_not_found
rescue_from ActionController::UnknownController, :with => :render_not_found
rescue_from ActionController::UnknownAction, :with => :render_not_found
private
def render_not_found(exception)
# logger.info(exception) # for logging
respond_to do |format|
render json: {:error => "404"}, status: 404
end
end
def render_error(exception)
# logger.info(exception) # for logging
respond_to do |format|
render json: {:error => "500"}, status: 500
end
end
public
def some_public_func
#do sthg
end
Pensé eso, pero ¿tendría que hacer un trabajo adicional para generar errores si se realizaran otras solicitudes? También me pregunté si rack sería un mejor lugar para esto. – Nick
Huelga que funciona muy bien :) Todavía me pregunto si el estante sería mejor. Tiempo para cavar – Nick
Esto hizo cero diferencia para mí. Solo importaba qué responder a las cosas que tenía que hacer en las acciones de mi controlador. –