您在動作中調用的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 :: Base類。
include ActionController::Redirecting
include ActionController::Rendering # <--
include ActionController::Renderers::All
在轉彎,ActionController::Rendering
包括AbstractController::Rendering
,你可以在ActionController::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
上滿鏈是
AbstractController::Base#render
--> super()
--> ActionController::Rendering#render
--> super()
--> AbstractController::Rendering#render
--> render_to_body
@artemave,你問18個問題,並只接受了其中的四個答案。如果答案對你有幫助,那麼請接受它。謝謝! – 2010-01-19 15:51:13