9

Ich habe den folgenden Code:rescue_from :: AbstractController :: ActionNotFound funktioniert nicht

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 

Sie alle arbeiten einwandfrei, mit Ausnahme :: AbstractController :: ActionNotFound

Ich habe auch versucht

AbstractController::ActionNotFound 
ActionController::UnknownAction 

Fehler:

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

Antwort

7

This similar question schlägt vor, dass Sie eine ActionNotFound Ausnahme nicht mehr abfangen können. Überprüfen Sie den Link für Problemumgehungen. This suggestion eine Rack-Middleware verwenden, um 404s zu fangen, sieht für mich am saubersten aus.

3

Um AbstractController::ActionNotFound in einem Controller zu retten, können Sie so etwas wie dies versuchen:

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 

Dies überschreibt die process Methode von AbstractController::Base die AbstractController::ActionNotFound (siehe source) erhöht.

0

Ich denke, wir sollten AbstractController::ActionNotFound in ApplicationController fangen. Ich habe versucht, dass scheint nicht zu funktionieren funktioniert.

rescue_from ActionController::ActionNotFound, with: :action_not_found 

Ich habe viel sauberen Weg gefunden, diese Ausnahme in ApplicationController zu handhaben. Um die Exception ActionNotFound in Ihrer Anwendung zu behandeln, müssen Sie die Methode action_missing in Ihrem Anwendungscontroller überschreiben.

def action_missing(m, *args, &block) 
    Rails.logger.error(m) 
    redirect_to not_found_path # update your application 404 path here 
end 

Lösung übernommen aus: coderwall handling exceptions in your rails application

0

process Aufschalten, wie Grégoire in seiner Antwort beschrieben, scheint zu funktionieren. Der Rails-Code besagt jedoch, stattdessen process_action zu überschreiben. Das funktioniert jedoch nicht, da process_action niemals aufgerufen wird, weil in process nach action_name gesucht wird.

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

Verwandte Themen