2011-06-23 38 views

回答

10

不要處理它。如果用戶輸入了錯誤的URL,那麼當您切換到生產環境時,他將收到404錯誤。

由於您處於開發環境中,您只會收到異常。

+0

我明白了。謝謝! +1 – Ben

0

你應該沒問題,可以使用默認的Rails處理,它將在公共目錄中加載404文件。

1

顯示404錯誤,用戶輸入了一個URL,在該URL中沒有定義要處理的Controller(或路由)中的相應Action。它應該像用戶輸入example.com/controller/jbsandfodsafoiuaudsfbsadf87basdfgsadfdsa一樣對待。

0

在生產模式下,異常將自動處理。 但有些情況下我們想處理異常。在我們的例子中,我們爲這些頁面提供了特殊的佈局。

在應用控制器,你必須拯救例外是這樣的:

PAGE_NOT_FOUND_ERRORS = ActiveRecord::RecordNotFound, ActionController::RoutingError, ActionController::UnknownAction, ActionController::UnknownController 
    def rescue_action_in_public(exception) 
    case exception 
    when *PAGE_NOT_FOUND_ERRORS 
     render_404 
    else 
     render_500 
    end 
    end 


    def render_404 
    render :file => File.join(RAILS_ROOT, 'public', '404.html'), :status => 404, :layout => true 
    end 



    def render_500 
    render :file => File.join(RAILS_ROOT, 'public', '500.html'), :status => 500 
    end 
1

config/environments/development.rb,關閉consider_all_requests_local並重新啓動服務器。現在你會看到錯誤頁面。完成設計後,再打開consider_all_requests_local並重新啓動服務器。

在生產中,人們將獲得您設計的404頁面。在開發中,您可以看到堆棧跟蹤,以便您可以調試自己的錯誤。