9

を働いていない私は、次のコードをしている:rescue_from :: AbstractController :: ActionNotFoundは

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): 

答えて

7

This similar questionは、もはやActionNotFound例外をキャッチできないことを示しています。回避策のリンクを確認してください。 This suggestion Rackミドルウェアを使用して404をキャッチすると、私には一番きれいに見えます。コントローラにAbstractController::ActionNotFoundを救出する

3

は、あなたがこのような何かを試すことができます:

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 

これは(sourceを参照)AbstractController::ActionNotFoundを上げるAbstractController::Baseprocessメソッドをオーバーライドします。

0

AbstractController::ActionNotFoundApplicationControllerにキャッチしてください。私はそれに続いて試してみたは動作していないようです

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 
end 

ソリューション:彼の答えにグレゴワールによって記載されているように、processをオーバーライドcoderwall handling exceptions in your rails application

関連する問題