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.
rescue_from ::AbstractController::ActionNotFound not working
-
30-11-2021 - |
Domanda
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):
Soluzione
Altri suggerimenti
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).
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
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