Dynamic error pages in Rails 3

前端 未结 4 2038
抹茶落季
抹茶落季 2020-12-02 08:34

In Rails 2.3.x, you can override render_optional_error_file like so:

# ApplicationController.rb
protected
  def render_optional_error_file(statu         


        
相关标签:
4条回答
  • 2020-12-02 09:22

    In rails 3.2, it's easier than that:

    Add this to config/application.rb:

    config.exceptions_app = self.routes
    

    That causes errors to be routed via the router. Then you just add to config/routes.rb:

    match "/404", :to => "errors#not_found"
    

    I got this info from item #3 on the blog post "My five favorite hidden features in Rails 3.2" by By José Valim.

    0 讨论(0)
  • 2020-12-02 09:33

    The exception notifier has a method called notify_about_exception to initiate the error notification on demand.

    class ApplicationController < ActionController::Base
      include ExceptionNotification::Notifiable
    
      rescue_from Exception, :with => :render_all_errors
    
      def render_all_errors(e)
        log_error(e) # log the error
        notify_about_exception(e) # send the error notification
    
        # now handle the page
        if e.is_a?(ActionController::RoutingError)
          render_404(e)
        else
          render_other_error(e)
        end
      end
    
      def render_404_error(e)
       # your code
      end
    
      def render_other_error(e)
       # your code
      end
    end
    
    0 讨论(0)
  • 2020-12-02 09:35

    I would suggest using rescue_from instead. You would just rescue from specific errors rather than overriding rescue_action_in_public. This is especially useful when dealing with user-defined errors or controller-specific errors.

    # ApplicationController
    rescue_from ActionController::RoutingError, :with => :render_404
    rescue_from ActionController::UnknownAction, :with => :render_404
    rescue_from ActiveRecord::RecordNotFound, :with => :render_404
    rescue_from MyApp::CustomError, :with => :custom_error_resolution
    
    def render_404
      if /(jpe?g|png|gif)/i === request.path
        render :text => "404 Not Found", :status => 404
      else
        render :template => "shared/404", :layout => 'application', :status => 404
      end
    end
    
    # UsersController
    rescue_from MyApp::SomeReallySpecificUserError, :with => :user_controller_resolution
    

    http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html

    0 讨论(0)
  • 2020-12-02 09:36

    I have also faced such problem. The problem is because of attachment_fu gem or plugin. Just uninstall it and use any other plugin or gem will solve your problem.

    0 讨论(0)
提交回复
热议问题