9

I have the following code:

unless Rails.application.config.consider_all_requests_local
  rescue_from Exception, with: :render_exception
  rescue_from ActiveRecord::RecordNotFound, with: :render_exception
  rescue_from ActionController::UnknownController, with: :render_exception
  rescue_from ::AbstractController::ActionNotFound, with: :render_exception
  rescue_from ActiveRecord::ActiveRecordError, with: :render_exception
  rescue_from NoMethodError, with: :render_exception
end

They all work flawless, except ::AbstractController::ActionNotFound

I've also tried

AbstractController::ActionNotFound
ActionController::UnknownAction

error:

   AbstractController::ActionNotFound (The action 'show' could not be found for ProductsController):
vladCovaliov
  • 4,333
  • 2
  • 43
  • 58

4 Answers4

7

This similar question suggests that you can no longer catch an ActionNotFound exception. Check the link for workarounds. This suggestion to use a Rack middleware to catch 404s looks the cleanest to me.

Community
  • 1
  • 1
Buck Doyle
  • 6,333
  • 1
  • 22
  • 35
3

To rescue AbstractController::ActionNotFound in a controller, you can try something like this:

class UsersController < ApplicationController

  private

  def process(action, *args)
    super
  rescue AbstractController::ActionNotFound
    respond_to do |format|
      format.html { render :404, status: :not_found }
      format.all { render nothing: true, status: :not_found }
    end
  end


  public

  # actions must not be private

end

This overrides the process method of AbstractController::Base that raises AbstractController::ActionNotFound (see source).

user1003545
  • 2,078
  • 1
  • 15
  • 15
0

I think we should catch AbstractController::ActionNotFound in ApplicationController. I have tried following that does not appears to be working.

rescue_from ActionController::ActionNotFound, with: :action_not_found

I have found much cleaner way to handle this exception in ApplicationController. To handle the ActionNotFound Exception in your application, you have to override the action_missing method in your application controller.

def action_missing(m, *args, &block)
  Rails.logger.error(m)
  redirect_to not_found_path # update your application 404 path here
end

Solution Adapted from: coderwall handling exceptions in your rails application

Aamir
  • 16,329
  • 10
  • 59
  • 65
0

Overriding process, as described by Grégoire in his answer, seems to work. However, the Rails code says to instead override process_action. That doesn't work, though, because process_action never gets called due to the check for action_name in process.

https://github.com/rails/rails/blob/v3.2.21/actionpack/lib/abstract_controller/base.rb#L115

https://github.com/rails/rails/blob/v3.2.21/actionpack/lib/abstract_controller/base.rb#L161

mstrom
  • 1,655
  • 3
  • 26
  • 41