yangzhihuan 发表于 2013-2-7 18:51:34

rails下自定义出错页面

rails出错的时候,一般都是跳转到错误跟踪的页面下,这样在开发环境下是很好的,但是对正式环境就不是太友好了,所以如果你想自定义一下出错的页面,还是有办法的.

我一般都会在lib目录下新建一个ooxx.rb文件存放下面的代码,然后在environment.rb下加载一下.

environment.rb
require File.join(File.dirname(__FILE__),'..', 'lib','ooxx')

ooxx.rb
module ActionControllermodule Rescue    protected    #自定义的错误处理    def rescue_action_in_public(exception)      logger.error("rescue_action_in_public executed")      # 这里就是你可以折腾的地方      case exception      when ActiveRecord::RecordNotFound, ::ActionController::RoutingError,      ::ActionController::UnknownAction      logger.error("404 displayed")      render(:file=> "#{RAILS_ROOT}/public/404.html",               :status   => "404 Not Found")      else      logger.error("500 displayed")      render(:file=> "#{RAILS_ROOT}/public/500.html",               :status   => "500 Error")      #      SystemNotifier.deliver_exception_notification(self, request,      #                                                    exception)      end    endendend

请注意做完以上操作后,在开发环境下,照样会跳转到错误跟踪页面的,这是因为这个方法是否起作用取决于development.rb和production.rb中的配置:
# in production.rbconfig.action_controller.consider_all_requests_local = false# in development.rbconfig.action_controller.consider_all_requests_local = true
页: [1]
查看完整版本: rails下自定义出错页面