1

What is the correct way to assign a controller action or middleware to handle all exceptions (globally) and strict parameter errors in Rails 4?

Keep in mind, I don't want to do it just for one controller.

2 Answers 2

1

Well, the obvious solution is the application controller (/app/controllers/application_controller.rb), should work fine if your controllers have the 7 INCSEUD actions.

Sign up to request clarification or add additional context in comments.

Comments

0

Use exceptions_app.

application.rb

class Application < Rails::Application
   ...
   config.exceptions_app = lambda do |env|
        ExceptionController.action(:render_error).call(env)
   end
   ...
end

exception_controller.rb

class ExceptionController < ActionController::Base
    layout 'application'

    def render_error
        @exception = env["action_dispatch.exception"]
        @status_code = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
        render :error_page, status: @status_code, layout: true
    end
end

Taken from Rails 3.2 error handling with exceptions_app (Example)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.