Rails 오류 처리 인터페이스

1930 단어 htmlActiveRecordRails
1.runtime error에 대한 일반적인 처리
 
around_filter :rescue_record_not_found   
  
def rescue_record_not_found   
  begin   
    yield   
  rescue ActiveRecord::RecordNotFound   
    render :file => "#{RAILS_ROOT}/public/404.html"  
  end   
end  

 
 
2. 이렇게 처리할 수도 있다
 
rescue_from ActiveRecord::RecordNotFound, with => :rescue_record_not_found   
  
def rescue_record_not_found   
  render :file => "#{RAILS_ROOT}/public/404.html"  
end  

 
 
3. 다른 유형의 오류는 다음과 같이 참조하십시오.
    DEFAULT_RESCUE_RESPONSE = :internal_server_error
    DEFAULT_RESCUE_RESPONSES = {
      'ActionController::RoutingError'             => :not_found,
      'ActionController::UnknownAction'            => :not_found,
      'ActiveRecord::RecordNotFound'               => :not_found,
      'ActiveRecord::StaleObjectError'             => :conflict,
      'ActiveRecord::RecordInvalid'                => :unprocessable_entity,
      'ActiveRecord::RecordNotSaved'               => :unprocessable_entity,
      'ActionController::MethodNotAllowed'         => :method_not_allowed,
      'ActionController::NotImplemented'           => :not_implemented,
      'ActionController::InvalidAuthenticityToken' => :unprocessable_entity
    }

    DEFAULT_RESCUE_TEMPLATE = 'diagnostics'
    DEFAULT_RESCUE_TEMPLATES = {
      'ActionView::MissingTemplate'       => 'missing_template',
      'ActionController::RoutingError'    => 'routing_error',
      'ActionController::UnknownAction'   => 'unknown_action',
      'ActionView::TemplateError'         => 'template_error'
    }

 
 
 
 
 

좋은 웹페이지 즐겨찾기