Skip to content

Commit

Permalink
add rescue_from
Browse files Browse the repository at this point in the history
  • Loading branch information
dimanado committed Oct 5, 2015
1 parent 90a0059 commit cf79662
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
class ApplicationController < ActionController::Base
include DeviseTokenAuth::Concerns::SetUserByToken
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :null_session
include DeviseTokenAuth::Concerns::SetUserByToken

protect_from_forgery with: :null_session

rescue_from ActionController::ParameterMissing, with: :invalid_params
rescue_from ActiveRecord::RecordNotFound, with: :not_found

def render_error_messages(object, status=:unprocessable_entity)
render json: { :errors => object.errors.messages, :fields=>object.errors.keys },
status: status, head: status
end

def render_blank_success
render json: {}, status: :ok

This comment has been minimized.

Copy link
@harakhovich

harakhovich Oct 6, 2015

Contributor

я бы все-таки отправлял в боди success: true. Чтобы другим понятнее было.

This comment has been minimized.

Copy link
@dimanado

dimanado Oct 6, 2015

Author Owner

будет

end

def invalid_params(exception)
render json: { errors: "Required parameter is missing: #{exception.param}" }, status: :unprocessable_entity, head: :unprocessable_entity
end

def not_found
render json: { errors: "Item not found" }, status: :not_found, head: :not_found
end
end
2 changes: 1 addition & 1 deletion app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def index
def create
if course=Course.create(params.permit(:name))
current_user.courses<<course
render json: {}, status: :ok
render_blank_success
else
render json: {error: 'Course not create'}, status: 400
end
Expand Down

0 comments on commit cf79662

Please sign in to comment.