跳至內容 跳至搜尋
方法
#
R
V
已包含的模組

常數

DEFAULT_PROTECTED_INSTANCE_VARIABLES(預設受保護的實例變數) = %i(@_action_name @_response_body @_formats @_prefixes)
 

實例公開方法

render(*args, &block)

正規化參數和選項,然後委派給 render_to_body 並將結果放入 self.response_body 中。

支援的選項取決於底層的 render_to_body 實作。

# File actionpack/lib/abstract_controller/rendering.rb, line 26
def render(*args, &block)
  options = _normalize_render(*args, &block)
  rendered_body = render_to_body(options)
  if options[:html]
    _set_html_content_type
  else
    _set_rendered_content_type rendered_format
  end
  _set_vary_header
  self.response_body = rendered_body
end

render_to_body(options = {})

執行實際的模板渲染。

# File actionpack/lib/abstract_controller/rendering.rb, line 50
def render_to_body(options = {})
end

render_to_string(*args, &block)

類似於 render,但僅將渲染的模板作為字串返回,而不是設置 self.response_body

如果一個組件擴展了 response_body 的語義(如同 ActionController 將其擴展為任何響應 each 方法的東西),則需要覆蓋此方法才能仍然返回一個字串。

# File actionpack/lib/abstract_controller/rendering.rb, line 44
def render_to_string(*args, &block)
  options = _normalize_render(*args, &block)
  render_to_body(options)
end

rendered_format()

返回已渲染內容的 Content-Type(內容類型)。

# File actionpack/lib/abstract_controller/rendering.rb, line 54
def rendered_format
  Mime[:text]
end

view_assigns()

此方法應返回一個包含指派的雜湊。您可以覆蓋每個控制器的此配置。

# File actionpack/lib/abstract_controller/rendering.rb, line 62
def view_assigns
  variables = instance_variables - _protected_ivars

  variables.each_with_object({}) do |name, hash|
    hash[name.slice(1, name.length)] = instance_variable_get(name)
  end
end

實例私有方法

_normalize_args(action = nil, options = {})

通過將 render "foo" 轉換為 render action: "foo" 以及將 render "foo/bar" 轉換為 render file: "foo/bar" 來正規化參數。

# File actionpack/lib/abstract_controller/rendering.rb, line 73
def _normalize_args(action = nil, options = {}) # :doc:
  if action.respond_to?(:permitted?)
    if action.permitted?
      action
    else
      raise ArgumentError, "render parameters are not permitted"
    end
  elsif action.is_a?(Hash)
    action
  else
    options
  end
end

_normalize_options(options)

正規化選項。

# File actionpack/lib/abstract_controller/rendering.rb, line 88
def _normalize_options(options) # :doc:
  options
end

_process_options(options)

處理額外選項。

# File actionpack/lib/abstract_controller/rendering.rb, line 93
def _process_options(options) # :doc:
  options
end