跳至内容 跳至搜索
方法
#
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