濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Rails中遇到錯(cuò)誤跳轉(zhuǎn)到統(tǒng)一提示錯(cuò)誤頁(yè)的方法

Rails中遇到錯(cuò)誤跳轉(zhuǎn)到統(tǒng)一提示錯(cuò)誤頁(yè)的方法

熱門標(biāo)簽:天津外呼系統(tǒng)怎么收費(fèi) 巫師3地圖標(biāo)注魔力之所 AI電銷機(jī)器人 線路 漯河電銷 柯城手機(jī)地圖如何做地圖標(biāo)注 淮安自動(dòng)外呼系統(tǒng)供應(yīng)商 中牟外呼系統(tǒng)違法嗎 征服者企業(yè)地圖標(biāo)注 外呼線路從哪里出來的

一個(gè)迭代開發(fā)中的網(wǎng)站難免存在bug,出bug的時(shí)候客戶體驗(yàn)就很不好了,為解決此問題,可以在class error產(chǎn)生的時(shí)候,觸發(fā)跳轉(zhuǎn)到統(tǒng)一提示頁(yè)面,并給開發(fā)人員發(fā)郵件報(bào)錯(cuò)誤信息,提高測(cè)試能力和用戶體驗(yàn)。以下是核心方法;在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

標(biāo)簽:甘孜 內(nèi)江 棗莊 西雙版納 大慶 河池 南昌 克拉瑪依

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Rails中遇到錯(cuò)誤跳轉(zhuǎn)到統(tǒng)一提示錯(cuò)誤頁(yè)的方法》,本文關(guān)鍵詞  Rails,中,遇到,錯(cuò)誤,跳,轉(zhuǎn)到,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Rails中遇到錯(cuò)誤跳轉(zhuǎn)到統(tǒng)一提示錯(cuò)誤頁(yè)的方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Rails中遇到錯(cuò)誤跳轉(zhuǎn)到統(tǒng)一提示錯(cuò)誤頁(yè)的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    宜兴市| 北辰区| 临朐县| 宜昌市| 弥勒县| 通许县| 安溪县| 东光县| 建水县| 绍兴县| 梨树县| 福泉市| 陵川县| 济宁市| 玛沁县| 莱州市| 金塔县| 区。| 邻水| 璧山县| 湖南省| 左云县| 武义县| 公主岭市| 寿宁县| 乐清市| 农安县| 湟源县| 五指山市| 卫辉市| 天津市| 天峨县| 日照市| 饶阳县| 小金县| 武清区| 青岛市| 余干县| 收藏| 崇文区| 桦川县|