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 अपवाद नहीं पकड़ सकते हैं। कामकाज के लिए लिंक की जांच करें। 404s पकड़ने के लिए रैक मिडलवेयर का उपयोग करने के लिए This suggestion मुझे सबसे साफ दिखता है।

3

एक नियंत्रक में 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 

यह उपरोक्त कि AbstractController::ActionNotFound को जन्म देती है (source देखें) AbstractController::Base की process विधि।

0

मुझे लगता है कि हमें ApplicationController में पकड़ना चाहिए। मैंने कोशिश की है कि काम नहीं कर रहा है।

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 

समाधान से अनुकूलित: के रूप में अपने जवाब में Grégoire वर्णन करते हैं, coderwall handling exceptions in your rails application

0

process ओवरराइड करना, काम करने के लिए लगता है। हालांकि, रेल कोड process_action को ओवरराइड करने के लिए कहता है। यह काम नहीं करता है, हालांकि, process_actionprocess में 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#L161