Rails 3.1错误捕获

时间:2011-05-25 01:33:10

标签: ruby-on-rails ruby

我认为Rails 3.1正在改变引发错误的方式。任何人都可以协助或确认吗?我正在尝试使用Rails 3.1.0.rc1

创建自定义错误页面
unless config.consider_all_requests_local
    rescue_from Exception, :with => :render_error
    rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
    rescue_from ActionController::RoutingError, :with => :render_not_found
    rescue_from ActionController::UnknownController, :with => :render_not_found
    rescue_from ActionController::UnknownAction, :with => :render_not_found
end

^^这不起作用。

config.consider_all_requests_local       = true

默认情况下,这是在我的开发环境中。我假设Rails 3.1删除了“action_controller”,但我无法在任何地方确认。

谢谢!

2 个答案:

答案 0 :(得分:19)

我假设您的ApplicationController中出现以下代码?

unless config.consider_all_requests_local
  rescue_from Exception, :with => :render_error
  rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
  rescue_from ActionController::RoutingError, :with => :render_not_found
  rescue_from ActionController::UnknownController, :with => :render_not_found
  rescue_from ActionController::UnknownAction, :with => :render_not_found
end

如果是,请尝试替换此行:

unless config.consider_all_requests_local

有了这一行(我认为是前Rails 3):

unless ActionController::Base.consider_all_requests_local

或者这个(发布Rails 3):

unless Rails.application.config.consider_all_requests_local

答案 1 :(得分:9)

我不相信Matt的解决方案会在Rails 3.0 / 3.1中捕获路由错误。

尝试将以下内容放入application.rb:

# 404 catch all route
config.after_initialize do |app|
  app.routes.append{ match '*a', :to => 'application#render_not_found' } unless config.consider_all_requests_local
end

请参阅:https://github.com/rails/rails/issues/671#issuecomment-1780159

对我来说工作得很好!