ruby-on-rails - Rails 3.1 错误捕获

标签 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”,但我无法在任何地方确认这一点。

谢谢!

最佳答案

我假设以下代码出现在您的 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

关于ruby-on-rails - Rails 3.1 错误捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6118626/

相关文章:

javascript - 当不再选择值时反转更改(指定 ID)

ruby-on-rails - 是否有专门针对 rails 5.2.3 的 Rubocop 配置?

ruby - 使用 Eventmachine Http Request 时获取有关错误的信息

ruby-on-rails - 跳过与 simple_form 关联的包装器

ruby - Elasticsearch 字段提升未显示在映射中

ruby-on-rails - 使用 rails 显示来自数据库的信息不起作用

ruby-on-rails - Rails 生成的 session key 真的不受预测攻击的影响吗

ruby-on-rails - Assets 的静态 URL

ruby-on-rails - 从 Rails 应用程序(Word、PDF、Excel 等)中搜索附件

ruby-on-rails - 如何更改 shoulda 匹配器的主题