복붙노트

[RUBY-ON-RAILS] 레일의 동적 오류 페이지 3

RUBY-ON-RAILS

레일의 동적 오류 페이지 3

레일 인 2.3.x에서는과 같이 render_optional_error_file 대체 할 수 있습니다 :

# ApplicationController.rb
protected
  def render_optional_error_file(status_code)
    render :template => "errors/500", :status => 500, :layout => 'application'
  end

그러나 3가 더 이상 render_optional_error_file가 레일 없습니다. 대신, 당신은 당신과 같이 할 수 rescue_action_in_public를 재정의해야합니다 :

# config/initializers/error_page.rb
module ActionDispatch
  class ShowExceptions

    protected    
      def rescue_action_in_public(exception)
        status = status_code(exception).to_s

        template = ActionView::Base.new(["#{Rails.root}/app/views"])
        if ["404"].include?(status)
          file = "/errors/404.html.erb"
        else
          file = "/errors/500.html.erb"
        end        
        body = template.render(:file => file)

        render(status, body)
      end

  end
end

이것은 작동하지만, 응용 프로그램의 레이아웃을 사용하지 않습니다. 그러나, 당신이 그렇게 같은 레이아웃 경로를 지정하는 경우 :

body = template.render(:file => file, :layout => "layouts/application") # line 15

ActionView :: 템플릿 : 오류 : 당신은 안전 장치 응답하는 동안 오류가 발생합니다.

application.html.erb의 행 4 : 4이다 :

<%= stylesheet_link_tag "app", "jquery-ui", "jquery.fancybox", :cache => "all" %>

무엇이든 ActionView는 일반적으로로드 점점되지 템플릿을 렌더링하는 데 사용합니다.

스택 추적 :

  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:794:in `join'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:794:in `rails_asset_id'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:817:in `rewrite_asset_path'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:746:in `compute_public_path'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:424:in `path_to_stylesheet'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:875:in `ensure_stylesheet_sources!'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:874:in `each'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:874:in `ensure_stylesheet_sources!'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/helpers/asset_tag_helper.rb:512:in `stylesheet_link_tag'
  /data/sites/fundraisers-stage/releases/20110316194843/app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___19482063_70294907435920_0'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/template.rb:135:in `send'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/template.rb:135:in `render'
  /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications.rb:54:in `instrument'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/template.rb:127:in `render'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/layouts.rb:80:in `_render_layout'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/rendering.rb:62:in `_render_template'
  /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications.rb:52:in `instrument'
  /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
  /var/lib/gems/1.8/gems/activesupport-3.0.5/lib/active_support/notifications.rb:52:in `instrument'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/rendering.rb:56:in `_render_template'
  /var/lib/gems/1.8/gems/actionpack-3.0.5/lib/action_view/render/rendering.rb:26:in `render'
  /data/sites/fundraisers-stage/releases/20110316194843/config/initializers/error_pages.rb:15:in `rescue_action_in_public'

해결법

  1. ==============================

    1.3.2 레일에서는보다 쉽게 ​​:

    3.2 레일에서는보다 쉽게 ​​:

    이것에 설정 / application.rb 추가 :

    config.exceptions_app = self.routes
    

    원인 오류가 라우터를 통해 라우팅 할 수있다. 그럼 당신은 단지에 설정 / routes.rb 추가 :

    match "/404", :to => "errors#not_found"
    

    나는함으로써 호세 Valim에 의해 블로그 게시물 "레일 3.2 나의 다섯 개 좋아하는 숨겨진 기능"에 대한 항목 # 3에서이 정보를 얻었다.

  2. ==============================

    2.내가 대신 rescue_from를 사용하는 것이 좋습니다 것입니다. 오히려 rescue_action_in_public를 오버라이드 (override)에 비해 특정 오류에서 당신이하는 것과 구조. 사용자 정의 오류 또는 컨트롤러 특정 오류를 처리 할 때 특히 유용합니다.

    내가 대신 rescue_from를 사용하는 것이 좋습니다 것입니다. 오히려 rescue_action_in_public를 오버라이드 (override)에 비해 특정 오류에서 당신이하는 것과 구조. 사용자 정의 오류 또는 컨트롤러 특정 오류를 처리 할 때 특히 유용합니다.

    # ApplicationController
    rescue_from ActionController::RoutingError, :with => :render_404
    rescue_from ActionController::UnknownAction, :with => :render_404
    rescue_from ActiveRecord::RecordNotFound, :with => :render_404
    rescue_from MyApp::CustomError, :with => :custom_error_resolution
    
    def render_404
      if /(jpe?g|png|gif)/i === request.path
        render :text => "404 Not Found", :status => 404
      else
        render :template => "shared/404", :layout => 'application', :status => 404
      end
    end
    
    # UsersController
    rescue_from MyApp::SomeReallySpecificUserError, :with => :user_controller_resolution
    

    http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html

  3. ==============================

    3.예외 통지 수요에 오류 알림을 시작 notify_about_exception라는 메소드가 있습니다.

    예외 통지 수요에 오류 알림을 시작 notify_about_exception라는 메소드가 있습니다.

    class ApplicationController < ActionController::Base
      include ExceptionNotification::Notifiable
    
      rescue_from Exception, :with => :render_all_errors
    
      def render_all_errors(e)
        log_error(e) # log the error
        notify_about_exception(e) # send the error notification
    
        # now handle the page
        if e.is_a?(ActionController::RoutingError)
          render_404(e)
        else
          render_other_error(e)
        end
      end
    
      def render_404_error(e)
       # your code
      end
    
      def render_other_error(e)
       # your code
      end
    end
    
  4. ==============================

    4.나는 또한 같은 문제에 직면했다. 인해 문제가 attachment_fu 보석 또는 플러그인이다. 그냥 제거하고 문제를 해결할 다른 플러그인이나 보석을 사용합니다.

    나는 또한 같은 문제에 직면했다. 인해 문제가 attachment_fu 보석 또는 플러그인이다. 그냥 제거하고 문제를 해결할 다른 플러그인이나 보석을 사용합니다.

  5. from https://stackoverflow.com/questions/5331008/dynamic-error-pages-in-rails-3 by cc-by-sa and MIT license