9

Ho il seguente codice:rescue_from :: :: AbstractController ActionNotFound non funziona

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 

Lavorano tutti impeccabile, tranne :: :: AbstractController ActionNotFound

Ho anche provato

AbstractController::ActionNotFound 
ActionController::UnknownAction 

errore:

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

risposta

7

This similar question suggerisce che non è più possibile rilevare un'eccezione ActionNotFound. Controlla il collegamento per soluzioni alternative. This suggestion utilizzare un middleware Rack per catturare 404s sembra il più pulito per me.

3

Per salvare AbstractController::ActionNotFound in un controllore, si può provare qualcosa di simile:

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 

Questo ridefinisce il metodo di AbstractController::Baseprocess che alza AbstractController::ActionNotFound (vedi source).

0

Penso che dovremmo prendere AbstractController::ActionNotFound in ApplicationController. Ho provato a seguire che non sembra funzionare.

rescue_from ActionController::ActionNotFound, with: :action_not_found 

ho trovato modo molto più pulito per gestire questa eccezione in ApplicationController. Per gestire l'eccezione ActionNotFound nell'applicazione, è necessario sovrascrivere il metodo action_missing nel controller dell'applicazione.

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

Soluzione Tratto da: coderwall handling exceptions in your rails application