読者です 読者をやめる 読者になる 読者になる

Rails上での例外の扱い方。

以下のようにApplicationControllerにrescue_action_in_publicを定義する。

class ApplicationController < ActionController::Base
  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_nikko_session_id'
  
  def rescue_action_in_public(exception)
    case exception
    when ::ActionController::RoutingError, ::ActionController::UnknownAction, ::ActiveRecord::RecordNotFound
      render :file=>"#{RAILS_ROOT}/public/404.html", :status=>'404 Not Found'
    else
      logger.error("Application Error!")
      render :file=>"#{RAILS_ROOT}/public/500.html", :status=>'500 Error'
    end
  end
  
  def local_request?
    false
  end
end