instance method render

Ruby on Rails edge

Since v7.0.10

Available in: v7.0.10 v7.1.6 v7.2.3.2 v8.0.5.1 v8.1.3.1 edge

Signature

render(*args)

Renders a template and assigns the result to self.response_body.

If no rendering mode option is specified, the template will be derived from the first argument.

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

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

If the first argument responds to +render_in+, the template will be rendered by calling +render_in+ with the current view context, render options, and block.

class Greeting
  def render_in(view_context, **)
    if block_given?
      view_context.render(html: yield)
    else
      view_context.render(inline: <<~ERB.strip, **)
        <%= Hello, <%= local_assigns.fetch(:name, "World") %>
      ERB
    end
  end

render(Greeting.new)                                        # => "Hello, World"
render(renderable: Greeting.new)                            # => "Hello, World"
render(Greeting.new, name: "Local")                         # => "Hello, Local"
render(renderable: Greeting.new, locals: { name: "Local" }) # => "Hello, Local"
render(Greeting.new) { "Hello, Block" }                     # => "Hello, Block"
render(renderable: Greeting.new) { "Hello, Block" }         # => "Hello, Block"
Rendering Mode
:partial

See ActionView::PartialRenderer for details.

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

Renders the contents of a file. This option should not be used with unsanitized user input.

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

Renders an ERB template string.

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

Renders the provided text, and sets the content type as text/plain.

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

Renders the provided text, and sets the content type as text/plain.

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

Renders the provided HTML string, and sets the content type as text/html. If the string is not html_safe?, performs HTML escaping on the string before rendering.

    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

Renders the provided object as JSON, and sets the content type as application/json. If the object is not a string, it will be converted to JSON by calling to_json.

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

Renders the provided object by calling render_in with the current view context, render options, and block. The response format is determined by calling format on the renderable if it responds to format, falling back to text/html by default.

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

    render renderable: Greeting.new, locals: { name: "Local" }
    # => renders "<h1>Hello, Local</h1>"

By default, when a rendering mode is specified, no layout template is rendered.

Options
:assigns

Hash of instance variable assignments for the template.

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

Hash of local variable assignments for the template.

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

The layout template to render. Can also be false or true to disable or (re)enable the default layout template.

    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

The HTTP status code to send with the response. Can be specified as a number or as the status name in Symbol form. Defaults to 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
:variants

This tells Rails to look for the first template matching any of the variations.

    render "posts/index", variants: [:mobile]
    # => renders app/views/posts/index.html+mobile.erb

Parameters

args rest
Source
# File actionpack/lib/action_controller/metal/rendering.rb, line 176
    def render(*args)
      raise ::AbstractController::DoubleRenderError if response_body
      super
    end

Defined in actionpack/lib/action_controller/metal/rendering.rb line 176 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionController::Rendering

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close