【问题标题】:How can Rails automatically rescue from ActiveRecord::RecordNotFound in development mode?Rails 如何在开发模式下自动从 ActiveRecord::RecordNotFound 中救援?
【发布时间】:2012-04-11 15:39:25
【问题描述】:

我在我的ApplicationController 上使用以下代码:

rescue_from ActiveRecord::RecordNotFound, :with => :not_found

def not_found
  render :nothing => true, :status => :not_found
end

为了让 Rails 以正确的状态码响应而不引发异常 处于开发模式。

我知道how-to-handle-errors-like-404-500-in-rails3 解释了 Rails 如何在生产模式下工作。换句话说,我在开发模式下尝试做的事情是在生产模式下没有任何代码。

我怎样才能让开发模式表现得像生产模式一样,摆脱上面的代码?

请注意,我使用的是Rails 3.2.3

【问题讨论】:

    标签: ruby-on-rails-3 rescue


    【解决方案1】:

    也许您想关闭它?:

    config.consider_all_requests_local       = false
    

    config/environments/development.rb 中默认设置为true

    【讨论】:

    • 谢谢。我相信你的答案是正确的,我会接受。您能否在答案中添加必须删除文件“public/404.html”?这使得问题的答案更加准确,因为在我当前的代码中,我没有渲染任何内容 => true。
    • 让我再研究一下这个主题。
    • 有这方面的消息吗?你研究过这个主题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2014-11-08
    相关资源
    最近更新 更多