跳至内容 跳至搜索
命名空间
方法
R

常量

RENDER_FORMATS_IN_PRIORITY = [:body, :plain, :html]
 

实例公共方法

render(*args)

渲染模板并将结果分配给self.response_body

如果未指定渲染模式选项,则将从第一个参数派生模板。

render "posts/show"
# => renders app/views/posts/show.html.erb

# In a PostsController action...
render :show
# => renders app/views/posts/show.html.erb

如果第一个参数响应render_in,则将通过调用render_in(使用当前视图上下文)来渲染模板。

class Greeting
  def render_in(view_context)
    view_context.render html: "<h1>Hello, World</h1>"
  end

  def format
    :html
  end
end

render(Greeting.new)
# => "<h1>Hello, World</h1>"

render(renderable: Greeting.new)
# => "<h1>Hello, World</h1>"

Rendering 模式

:partial

有关详细信息,请参阅ActionView::PartialRenderer

    render partial: "posts/form", locals: { post: Post.new }
    # => renders app/views/posts/_form.html.erb
:file

渲染文件的内容。此选项不应与未经消毒的用户输入一起使用。

    render file: "/path/to/some/file"
    # => renders /path/to/some/file
:inline

渲染一个ERB 模板字符串。

    @name = "World"
    render inline: "<h1>Hello, <%= @name %>!</h1>"
    # => renders "<h1>Hello, World!</h1>"
:body

渲染提供的文本,并将内容类型设置为text/plain

    render body: "Hello, World!"
    # => renders "Hello, World!"
:plain

渲染提供的文本,并将内容类型设置为text/plain

    render plain: "Hello, World!"
    # => renders "Hello, World!"
:html

渲染提供的 HTML 字符串,并将内容类型设置为text/html。如果该字符串不是html_safe?,则在渲染之前对该字符串执行 HTML 转义。

    render html: "<h1>Hello, World!</h1>".html_safe
    # => renders "<h1>Hello, World!</h1>"

    render html: "<h1>Hello, World!</h1>"
    # => renders "&lt;h1&gt;Hello, World!&lt;/h1&gt;"
:json

将提供的对象渲染为 JSON,并将内容类型设置为application/json。如果该对象不是字符串,则将通过调用to_json将其转换为 JSON。

    render json: { hello: "world" }
    # => renders "{\"hello\":\"world\"}"
:renderable

通过使用当前视图上下文调用render_in来渲染提供的对象。响应格式是通过在可渲染对象上调用format(如果它响应format)来确定的,默认情况下回退到text/html

    render renderable: Greeting.new
    # => renders "<h1>Hello, World</h1>"

默认情况下,当指定渲染模式时,不会渲染任何布局模板。

选项

:assigns

模板的实例变量赋值的Hash

    render inline: "<h1>Hello, <%= @name %>!</h1>", assigns: { name: "World" }
    # => renders "<h1>Hello, World!</h1>"
:locals

模板的局部变量赋值的Hash

    render inline: "<h1>Hello, <%= name %>!</h1>", locals: { name: "World" }
    # => renders "<h1>Hello, World!</h1>"
:layout

要渲染的布局模板。也可以是falsetrue,以禁用或(重新)启用默认布局模板。

    render "posts/show", layout: "holiday"
    # => renders app/views/posts/show.html.erb with the app/views/layouts/holiday.html.erb layout

    render "posts/show", layout: false
    # => renders app/views/posts/show.html.erb with no layout

    render inline: "<h1>Hello, World!</h1>", layout: true
    # => renders "<h1>Hello, World!</h1>" with the default layout
:status

与响应一起发送的 HTTP 状态代码。可以指定为数字或Symbol形式的状态名称。默认为 200。

    render "posts/new", status: 422
    # => renders app/views/posts/new.html.erb with HTTP status code 422

    render "posts/new", status: :unprocessable_entity
    # => renders app/views/posts/new.html.erb with HTTP status code 422
# File actionpack/lib/action_controller/metal/rendering.rb, line 165
def render(*args)
  raise ::AbstractController::DoubleRenderError if response_body
  super
end

render_to_string(*)

render类似,但仅将渲染的模板作为字符串返回,而不是设置self.response_body

# File actionpack/lib/action_controller/metal/rendering.rb, line 174
def render_to_string(*)
  result = super
  if result.respond_to?(:each)
    string = +""
    result.each { |r| string << r }
    string
  else
    result
  end
end