国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Ruby > 正文

Rails之遇到錯(cuò)誤跳轉(zhuǎn)頁的方法

2020-02-24 15:39:56
字體:
供稿:網(wǎng)友

我們可以在生成類錯(cuò)誤時(shí)觸發(fā)跳轉(zhuǎn)到統(tǒng)一的提示頁,并向開發(fā)人員發(fā)送電子郵件來報(bào)告錯(cuò)誤信息,從而提高測試能力和用戶體驗(yàn),今天武林技術(shù)頻道小編找到了解決方法,下面一起來看看吧!

以下是核心方法;在ApplicationController中添加如下代碼,不同rails版本的class error略有變化。

?

復(fù)制代碼 代碼如下:

AR_ERROR_CLASSES = [ActiveRecord::RecordNotFound, ActiveRecord::StatementInvalid]?
? ERROR_CLASSES = [NameError, NoMethodError, RuntimeError,?
???????? ActionView::TemplateError,?
???????? ActiveRecord::StaleObjectError, ActionController::RoutingError,?
???????? ActionController::UnknownController, AbstractController::ActionNotFound,?
???????? ActionController::MethodNotAllowed, ActionController::InvalidAuthenticityToken]?
?
? ACCESS_DENIED_CLASSES = [CanCan::AccessDenied]?
?
? if Rails.env.production??
??? rescue_from *AR_ERROR_CLASSES, :with => :render_ar_error?
??? rescue_from *ERROR_CLASSES, :with => :render_error?
??? rescue_from *ACCESS_DENIED_CLASSES, :with => :render_access_denied?
? end?
???
? #called by last route matching unmatched routes.? Raises RoutingError which will be rescued from in the same way as other exceptions.?
?
#備注rails3.1后ActionController::RoutingError在routes.rb中最后加如下代碼才能catch了。?
#rails3下:match '*unmatched_route', :to => 'application#raise_not_found!'?
#rails4下:get '*unmatched_route', :to => 'application#raise_not_found!'?
?
? def raise_not_found!?
??? raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}")?
? end?
?
? def render_ar_error(exception)?
??? case exception?
??? when *AR_ERROR_CLASSES then exception_class = exception.class.to_s?
??? else exception_class = 'Exception'?
??? end?
?
??? send_error_email(exception, exception_class)?
? end?
?
? def render_error(exception)?
??? case exception?
??? when *ERROR_CLASSES then exception_class = exception.class.to_s?
??? else exception_class = 'Exception'?
??? end?
?
??? send_error_email(exception, exception_class)?
? end?
?
? def render_access_denied(exception)?
??? case exception?
??? when *ACCESS_DENIED_CLASSES then exception_class = exception.class.to_s?
??? else exception_class = "Exception"?
??? end?
?
??? send_error_email(exception, exception_class)?
? end

Rails之遇到錯(cuò)誤跳轉(zhuǎn)頁的方法就為大家介紹到這里了,跟多的相關(guān)知識(shí)盡在武林技術(shù)頻道!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 海南省| 墨竹工卡县| 铜梁县| 大同县| 西吉县| 南昌县| 工布江达县| 邵武市| 拜城县| 马龙县| 佛教| 六安市| 宜章县| 鹤峰县| 连江县| 河南省| 临西县| 公安县| 资源县| 铜川市| 类乌齐县| 黄石市| 定安县| 南城县| 南投县| 禹州市| 涿州市| 团风县| 扎赉特旗| 枝江市| 平远县| 文安县| 万盛区| 嵩明县| 朝阳市| 治多县| 田东县| 丁青县| 汾阳市| 龙岩市| 绥芬河市|