2011-07-18 110 views
1

我運行的Rails 3.0.8與使用WEBrick Web服務器在生產模式開始這樣的命令隱藏所有異常消息

RAILS_ENV=production rails server 

我有以下問題。

我讀過,生產模式下的rails應該處理所有的異常和錯誤。 但我實際上仍然有錯誤消息「ActiveRecord :: RecordNotFound」,當我試圖在生產模式中獲取未包含的項目。

我也看到了有關

rescue_from ActiveRecord::RecordNotFound, :with => :page_not_found 

這樣的黑客,但我認爲這不是一個Rails的方式。

這裏是我的production.rb文件內容:

BeerPub::Application.configure do 
    # Settings specified here will take precedence over those in config/application.rb 

    # The production environment is meant for finished, "live" apps. 
    # Code is not reloaded between requests 
    config.cache_classes = true 
    config.whiny_nils = false 

    # Full error reports are disabled and caching is turned on 
    config.consider_all_requests_local  = false 
    config.action_view.debug_rjs    = false 
    config.action_controller.perform_caching = true 

    # Specifies the header that your server uses for sending files 
    config.action_dispatch.x_sendfile_header = "X-Sendfile" 

    # For nginx: 
    # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' 

    # If you have no front-end server that supports something like X-Sendfile, 
    # just comment this out and Rails will serve the files 

    # See everything in the log (default is :info) 
    # config.log_level = :debug 

    # Use a different logger for distributed setups 
    # config.logger = SyslogLogger.new 

    # Use a different cache store in production 
    # config.cache_store = :mem_cache_store 

    # Disable Rails's static asset server 
    # In production, Apache or nginx will already do this 
    config.serve_static_assets = true 

    # Enable serving of images, stylesheets, and javascripts from an asset server 
    # config.action_controller.asset_host = "http://assets.example.com" 

    # Disable delivery errors, bad email addresses will be ignored 
    # config.action_mailer.raise_delivery_errors = false 

    # Enable threaded mode 
    # config.threadsafe! 

    # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 
    # the I18n.default_locale when a translation can not be found) 
    config.i18n.fallbacks = true 

    # Send deprecation notices to registered listeners 
    config.active_support.deprecation = :notify 
end 

正如你可以看到,這是很平常的。

請幫我解決這個問題。

UPD:

我還可以得到下面的錯誤

Routing Error 

No route matches "/lol" 

這是另一種類型的異常,但問題是一樣的。處理這種情況的最佳方式是什麼?

回答

0

「我讀過,生產模式下的導軌應該處理所有異常和錯誤。」

這是不正確的,Rails不會catch例外,但它只是在生產模式下,你有不同的方式來處理它們。

你寫的rescue_from方法絕對正確。

許多Rails開發人員並不在乎拯救RecordNotFound異常,因爲簡單的事實是,取決於應用程序,是做錯了事的用戶。

但有些應用程序喜歡捕獲此異常並執行自定義操作,例如重定向,呈現文本或不同視圖。