简体   繁体   English

rescue_from :: AbstractController :: ActionNotFound无效

[英]rescue_from ::AbstractController::ActionNotFound not working

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 它们都完美无缺,除了:: AbstractController :: ActionNotFound

I've also tried 我也试过了

AbstractController::ActionNotFound
ActionController::UnknownAction

error: 错误:

   AbstractController::ActionNotFound (The action 'show' could not be found for ProductsController):

This similar question suggests that you can no longer catch an ActionNotFound exception. 这个类似的问题表明您无法再捕获ActionNotFound异常。 Check the link for workarounds. 检查链接以获取解决方法。 This suggestion to use a Rack middleware to catch 404s looks the cleanest to me. 使用Rack中间件来捕获404的这个建议看起来对我来说是最干净的。

To rescue AbstractController::ActionNotFound in a controller, you can try something like this: 要在控制器中挽救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

This overrides the process method of AbstractController::Base that raises AbstractController::ActionNotFound (see source ). 这将覆盖AbstractController::Baseprocess方法,该方法引发AbstractController::ActionNotFound (请参阅source )。

I think we should catch AbstractController::ActionNotFound in ApplicationController . 我想我们应该在ApplicationController捕获AbstractController::ActionNotFound 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 . 我发现在ApplicationController处理这个异常的方法更加清晰。 To handle the ActionNotFound Exception in your application, you have to override the action_missing method in your application controller. 要在应用程序中处理ActionNotFound Exception,必须覆盖应用程序控制器中的action_missing方法。

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 解决方案改编自: coderwall处理rails应用程序中的异常

Overriding process , as described by Grégoire in his answer, seems to work. Grégoire在他的回答中描述的压倒性process似乎有效。 However, the Rails code says to instead override process_action . 但是,Rails代码改为覆盖process_action That doesn't work, though, because process_action never gets called due to the check for action_name in process . 但这不起作用,因为process_action永远不会因为检查process action_name而被调用。

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#L115

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM