2

간단한 앱을 만들었으므로 404, 500 등의 http 오류 페이지를 테스트하고 싶습니다. 내 바우 입력하면레일, 개발 환경 및 오류 페이지

  1. ... 내 enviroments에/development.rb false로 config.consider_all_requests_local 변경했습니다하지만 난 당신에게 몇 가지 질문을하고 싶습니다 그래서 난 아직도 몇 가지 문제가있어 부적절한 것 같습니다. http://localhost:3000/products/dfgdgdgdgfd 나는 여전히 "알 수없는 액션"사이트를 보았습니다. 그러나 전 컴퓨터에 로컬 IP 주소를 입력하면됩니다. http://192.168.1.106:3000/products/dfgdgdgdgfd 공용 폴더에서 404 오류 페이지를 볼 수 있습니다. 왜 그런 일이 일어나는거야?

  2. 내 작은 프로젝트를 어딘가에 배치하면 내 앱이 프로덕션 모드를 사용하고 오류가 발생하면 404 또는 500 페이지가 표시된다는 것을 알고 있습니다. 그러나 이러한 오류 페이지를보다 동적으로 만들거나 (예 : 인기 제품 목록의 레이아웃을 사용하는 동안 오류 메시지를 렌더링하는 경우) 단순히 주 페이지로 리디렉션하려는 경우 어떻게해야합니까?

2.1.

unless Rails.application.config.consider_all_requests_local 
    rescue_from Exception, :with => :render_error 
    rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found 
    rescue_from AbstractController::ActionNotFound, :with => :render_not_found 
    rescue_from ActionController::RoutingError, :with => :render_not_found 
    rescue_from ActionController::UnknownController, :with => :render_not_found 
    rescue_from ActionController::UnknownAction, :with => :render_not_found 
end 
. 
. 
. 
private 
def render_error exception 
    Rails.logger.error(exception) 
    redirect_to root_path 
    #or 
    # render :controller=>'errors', :action=>'error_500', :status=>500 
end 

def render_not_found exception 
    Rails.logger.error(exception) 
    redirect_to root_path 
    #or 
    # render :controller=>'errors', :action=>'error_404', :status=>404 
end 

...하지만 그 코드는 어떤 경우에 작동하지 않았다 : 내가 찾은 첫 번째 솔루션은 응용 프로그램 컨트롤러에 rescue_from 방법을 사용하는 것이 었습니다.

2.2. 두 번째 해결 방법은 match "*path" , :to => "products#show", :id=>1 (내 바보 같은 응용 프로그램의 예제 메인 페이지) 또는 match "*path" , :to => "errors#error_404", :id=>1을 routes.rb 파일 끝에 붙여 넣는 것입니다. 이 코드는 http://192.168.1.106:3000/dfgdgdgdgfd과 같은 오타에서만 작동합니다. 컨트롤러가 있지만 동작을 찾을 수 없으므로 http://192.168.1.106:3000/products/dfgdgdgdgfd을 시도하면 404 페이지가 계속 남아 있기 때문입니다. match "*path/*act" , :to => "products#show", :id=>1 또는 match ":controller(/*act)" , :to => "products#show", :id=>8과 같이 약간의 노력을했지만 그 중 하나는 작동하지 않습니다.

2.3. 세 번째 솔루션은 오류 컨트롤러를 만드는 것이었다과 이니셜 라이저에있는 파일이 코드를 사용하여 폴더 : 그것은 어떤 레이아웃을 렌더링 아니에요 그것은 나를 동적 ERB 파일 만 렌더링 할 수 있도록 때문에 매우 유용했다

# initializers/error_pages.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)body = template.render(:partial => file, :layout => "layouts/application")으로 변경하려고 시도했지만 오류 만 있습니다.

내가 STH 잘못하고있어 알고 나는 그래서 당신이 도움이 될 수 있기를 바랍니다 그 오류 페이지에 대한 작업 솔루션이 있음을 보라 ...

건배. 응용 프로그램 컨트롤러에서

답변

5

당신은이 방법을 재정의해야 :

def method_missing(m, *args, &block) 
    Rails.logger.error(m) 
    redirect_to :controller=>"errors", :action=>"error_404" 
    # or render/redirect_to somewhere else 
end 

를 다음이 코드와 결합 할 수 있습니다

unless Rails.application.config.consider_all_requests_local 
    rescue_from Exception, :with => :method_missing 
    rescue_from ActiveRecord::RecordNotFound, :with => :method_missing 
    rescue_from AbstractController::ActionNotFound, :with => :method_missing 
    rescue_from ActionController::RoutingError, :with => :method_missing 
    rescue_from ActionController::UnknownController, :with => :method_missing 
    rescue_from ActionController::UnknownAction, :with => :method_missing 
end