Rails 3.0.15自定义404和500

时间:2012-11-02 17:11:15

标签: ruby-on-rails http-status-code-404 custom-error-pages

我已经看到很多关于Rails的自定义错误视图的问题,但还没有找到我的问题的解决方案!

对于404,现在我的routes.rb底部有一个catch-all来重定向未知的命名路由,例如“webroot / adsfsdfasdf /”,但它无效的ID无效,例如“webroot / people / x1df1231” - ActiveRecord :: RecordNotFound(找不到ID = x1df1231的人)

对于500,我还没有找到解决方案。

我目前无法升级Rails,

2 个答案:

答案 0 :(得分:1)

如果您想使用或apache或nginx在生产中执行此操作,您可以在服务器配置文件中设置它,而不是在rails中。

答案 1 :(得分:0)

我找到的最佳解决方案是使用“around_filter”

在application_controller之上:

around_filter :handle_errors

然后在

下面
def handle_errors
    yield
    rescue => e
        logger.debug "\n ====== ERROR ====== \n\n #{e.message} \n\n #{e.annoted_source_code} \n\n #{e.backtrace} \n\n ================= \n\n"
        if e.is_a?(ActiveRecord::RecordNotFound)
            render '/errors/e404'
        else
            render '/errors/e500'
        end
end

其中'/ errors / e404'是一个模板,例如views / errors / e404.html.haml 没有修改routes.rb。原始的全能路线有时会破坏申请。

相关问题