首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rescue_from ::AbstractController::ActionNotFound不工作

rescue_from ::AbstractController::ActionNotFound不工作
EN

Stack Overflow用户
提问于 2012-11-18 01:33:17
回答 4查看 4.4K关注 0票数 9

我有以下代码:

代码语言:javascript
复制
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

我也试过

代码语言:javascript
复制
AbstractController::ActionNotFound
ActionController::UnknownAction

错误:

代码语言:javascript
复制
   AbstractController::ActionNotFound (The action 'show' could not be found for ProductsController):
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-11-18 01:45:14

This similar question建议您不再捕获ActionNotFound异常。检查链接中的变通方法。在我看来,使用Rack中间件捕获404的This suggestion是最干净的。

票数 7
EN

Stack Overflow用户

发布于 2015-04-01 22:27:04

要在控制器中拯救AbstractController::ActionNotFound,您可以尝试如下所示:

代码语言:javascript
复制
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::ActionNotFoundAbstractController::Baseprocess方法(请参见source)。

票数 3
EN

Stack Overflow用户

发布于 2017-03-07 16:15:24

我想我们应该在ApplicationController抓到AbstractController::ActionNotFound。我已经尝试遵循似乎不能工作的

代码语言:javascript
复制
rescue_from ActionController::ActionNotFound, with: :action_not_found

我已经在ApplicationController中找到了更清晰的方法来处理这个异常。若要处理应用程序中的ActionNotFound异常,必须重写应用程序控制器中的action_missing方法。

代码语言:javascript
复制
def action_missing(m, *args, &block)
  Rails.logger.error(m)
  redirect_to not_found_path # update your application 404 path here
end

改编自:coderwall handling exceptions in your rails application

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13432987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档