我有以下代码:
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它们都可以完美地工作,除了::AbstractController::ActionNotFound
我也试过
AbstractController::ActionNotFound
ActionController::UnknownAction错误:
AbstractController::ActionNotFound (The action 'show' could not be found for ProductsController):发布于 2012-11-18 01:45:14
This similar question建议您不再捕获ActionNotFound异常。检查链接中的变通方法。在我看来,使用Rack中间件捕获404的This suggestion是最干净的。
发布于 2015-04-01 22:27:04
要在控制器中拯救AbstractController::ActionNotFound,您可以尝试如下所示:
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这将重写引发AbstractController::ActionNotFound的AbstractController::Base的process方法(请参见source)。
发布于 2017-03-07 16:15:24
我想我们应该在ApplicationController抓到AbstractController::ActionNotFound。我已经尝试遵循似乎不能工作的。
rescue_from ActionController::ActionNotFound, with: :action_not_found我已经在ApplicationController中找到了更清晰的方法来处理这个异常。若要处理应用程序中的ActionNotFound异常,必须重写应用程序控制器中的action_missing方法。
def action_missing(m, *args, &block)
Rails.logger.error(m)
redirect_to not_found_path # update your application 404 path here
endhttps://stackoverflow.com/questions/13432987
复制相似问题