2010-01-19 3 views
3

render()가 어디서 왔는지 설명 할 수 있습니까? ActionController :: Base? rails 3.0 : ActionController :: Base render()

내가 아니라 지금까지 그것을 아래로 추적 관리 :

ActionController :: 자료는 ActionController는 :: 렌더링 모듈() 메소드가 정의 렌더링이 포함되어 있습니다. 그러나이 정의는 의 render()를 호출하여 수퍼 클래스를 호출합니다. 슈퍼 클래스는 ActionController :: Metal입니다. 그것의 에서 AbstractController :: Base로부터 상속받습니다. 그 중 누구도 ()을 정의하거나 포함하지 않습니다.

이제는 AbstractController :: Rendering에서 온 것 같습니다. 그러나 나는 이 실제로 어떻게 포함되어 있는지 알지 못합니다.

+1

@artemave이다. 답변이 도움이 되었다면 동의하십시오. 감사! –

답변

8

동작에서 호출하는 render 메서드는 ActionController::Base에 정의되어 있습니다.

def render(action = nil, options = {}, &blk) 
    options = _normalize_options(action, options, &blk) 
    super(options) 
end 

이 방법은 ActionController::Rendering에 정의 된 render 메소드를 호출 super에 대한 호출을 전달합니다.

def render(options) 
    super 
    self.content_type ||= options[:_template].mime_type.to_s 
    response_body 
end 

ActionController::Rendering 효과적으로 base.rb 파일의 시작 부분에 ActionController :: 기본 클래스로 혼합 모듈입니다. 당신이 ActionController::Rendering 모듈 정의에서 볼 수 있듯이 회전에서

include ActionController::Redirecting 
include ActionController::Rendering # <-- 
include ActionController::Renderers::All 

, ActionController::RenderingAbstractController::Rendering이 포함되어 있습니다.

module ActionController 
    module Rendering 
    extend ActiveSupport::Concern 

    included do 
     include AbstractController::Rendering 
     include AbstractController::LocalizedCache 
    end 

AbstractController::Rendering 렌더 체인에서 최종 호출 방법이다 render 방법을 제공한다.

# Mostly abstracts the fact that calling render twice is a DoubleRenderError. 
# Delegates render_to_body and sticks the result in self.response_body. 
def render(*args) 
    if response_body 
    raise AbstractController::DoubleRenderError, "Can only render or redirect once per action" 
    end 

    self.response_body = render_to_body(*args) 
end 

전체 체인은, 당신이 18 개 질문을 한 단지 그들 중 넷에 대한 답변을 수락 한

AbstractController::Base#render 
--> super() 
--> ActionController::Rendering#render 
--> super() 
--> AbstractController::Rendering#render 
--> render_to_body 
+0

"super는 현재 클래스의 수퍼 클래스에있는 것과 같은 이름의 메소드를 호출합니다." ActionController :: Rendering은 ActionController :: Base의 부모 클래스가 아닙니다. 그것은 모듈이 포함되어 있습니다. 그러면 어떻게 슈퍼가 당신이 묘사 한대로 작동 할 수 있을까요? – artemave

+1

클래스 A가 모듈 B를 포함하면 B는 A의 조상이됩니다.'Object # ancestors'를 참조하십시오. A에서'super '를 호출하면 B를 포함하여 A 클래스의 모든 조상에서 같은 이름의 메소드를 검색합니다. 루비에서는 상속 및/또는 Mixins을 사용하여 클래스를 확장 할 수 있습니다. –

+0

이것은 분명히 오늘의 잡았다. – artemave

0

renderAbstractController::Rendering으로 정의됩니다. render_to_body을 호출하고 다른 메서드로 디스패치합니다. 토끼 덩어리 야, 그치?

+0

이것이 원래 질문에 어떻게 대답하는지 모르겠습니다. – artemave

관련 문제