2012-11-02 31 views
0

我見過很多關於Rails自定義錯誤視圖的問題,但還沒有找到解決我的問題的方法!Rails 3.0.15自定義404和500

對於404,現在我的routes.rb在底部有一個捕獲全部來重定向未知的命名路由,例如「webroot/adsfsdfasdf /」,但它失敗的無效id例如「webroot/people/x1df1231」 - ActiveRecord :: RecordNotFound(找不到人ID = x1df1231)

對於500,我還沒有找到解決方案呢。

我不能在瞬間提升的Rails,

回答

0

我已經找到了最好的解決辦法是在application_controller的頂部使用 「around_filter」

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。最初的全接觸路線有時會打破應用程序。

1

如果您想在生產環境中使用apache或nginx進行此操作,可以將其設置在服務器配置文件中,而不是在rails中。