国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表

圖片精選

主站蜘蛛池模板: 海淀区| 鄂温| 泰州市| 贵阳市| 建宁县| 涟源市| 沂源县| 济源市| 卓资县| 阿合奇县| 五莲县| 天门市| 湛江市| 密山市| 德清县| 宜昌市| 昂仁县| 邵东县| 保康县| 西和县| 武穴市| 琼中| 上犹县| 颍上县| 怀宁县| 图木舒克市| 吴堡县| 喀什市| 郎溪县| 涟源市| 清远市| 皮山县| 米林县| 牟定县| 湘乡市| 汶上县| 星子县| 延吉市| 横峰县| 晋宁县| 和静县|