9

çalışmıyor Aşağıdaki kod var: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 

Onlar hariç :: AbstractController :: ActionNotFound, tüm çalışma kusursuz

Ben de

AbstractController::ActionNotFound 
ActionController::UnknownAction 
denedim

hatası:

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

cevap

7

This similar question, artık bir ActionNotFound istisnasını yakalayamayacağınızı önerir. Geçici çözümler için bağlantıyı kontrol edin. This suggestion bir raf ara katman kullanmak için 404s bana en temiz görünüyor. bir denetleyici içinde AbstractController::ActionNotFound kurtarmak için

3

, böyle bir şey deneyebilirsiniz:

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 

Bu (source bakınız) AbstractController::ActionNotFound yükseltir AbstractController::Base ait process yöntemini geçersiz kılar.

0

ApplicationController numaralı telefondan AbstractController::ActionNotFound'u yakalamalıyız. Takip eden çalışıyor görünmüyor.

rescue_from ActionController::ActionNotFound, with: :action_not_found 

ben ApplicationController bu durum işlenecek daha temiz yol bulduk. Uygulamanızdaki ActionNotFound İstisnasını ele almak için, uygulama denetleyicinizde action_missing yöntemini geçersiz kılmanız gerekir.

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

Çözüm uyarlanan: onun cevabını Grégoire tarafından açıklandığı gibi, process geçersiz kılma coderwall handling exceptions in your rails application

İlgili konular