# Understanding the Render Method in Rails

*Published on June 05, 2025*

---


> This is the sixth post in the series on&nbsp;[Rails controllers](/tag/controllers).

When building Rails apps, we often take&nbsp;`render`&nbsp;for granted. It’s easy to let Rails implicitly render the default template or to drop in&nbsp;`render :new`&nbsp;to render a different template.

However, as you get deeper into Rails, you learn that there’s a lot more under the hood: whether you want to render JSON APIs, partials, custom inline ERB, or raw files, the&nbsp;`render`&nbsp;method is one of the most flexible and powerful tools in the Rails controller toolbox.

Let’s explore the various ways to use&nbsp;`render`&nbsp;in controller actions, starting from the basics, and then venturing into the lesser-known territory. Knowing all the ways&nbsp;`render`&nbsp;works helps you build better controllers, whether you’re building HTML pages, APIs, or admin panels. Once you’ve mastered the render API, you can keep your controllers focused, your templates clean, and your responses accurate.

_This post focuses on the external API of the_&nbsp;_`render`_&nbsp;_method. If you're curious about how it works behind the scenes by reading the Rails source code, check out this post:_ [Understanding the Rails Rendering Process](/rails-rendering-process)

## Default Behavior: Rendering the Action's View

In the controller actions, if you don't explicitly render, Rails will render the template with the name matching the name of the controller action. This is the most common case, and Rails handles it implicitly.

For example, if you have this controller:

```ruby
class PostsController < ApplicationController
  def index
    @posts = Post.all
  end
end
```

Rails will render the&nbsp;`app/views/posts/index.html.erb`&nbsp;view template, without you having to explicitly render anything. In fact, the&nbsp;`index`&nbsp;action is not even needed, and Rails will assume it by default.

This is one of the best examples of convention over configuration, a core principle of the Rails framework. This is the sort of magic people talk about when they say Rails is magic.

If you do want to be explicit, pass the name of the action as a symbol:

```ruby
render :index
```

A common use case is when the validation fails during a create action, and we need to re-render the form using the new action.

```ruby
def create
  @post = Post.new(post_params)

  if @post.save
    redirect_to @post, notice: "Post was successfully created."
  else
    render :new, status: :unprocessable_entity
  end
end
```

Remember that we're not redirecting to the&nbsp;`new`&nbsp;action above. The code under the&nbsp;`new`&nbsp;method won't be executed. We're simply rendering the&nbsp;`new`&nbsp;template.

You can also render another format like&nbsp;`.json.erb`:

```ruby
render :index, formats: :json
```

**Note:**&nbsp;Calling the render method doesn't magically halt execution of your controller's action method. Rails will throw the&nbsp;`DoubleRenderError`&nbsp;if render in the middle of the action without returning explicitly.

```ruby
def show
  @book = Book.find(params[:id])
  
  if @book.special?
    render action: "special_show"
  end
  
  render action: "regular_show"
end
```

If you don't want to execute the rest of the code that follows, return early.

```ruby
def show
  @book = Book.find(params[:id])
  
  if @book.special?
    render action: "special_show"
    return
  end
  
  render action: "regular_show"
end
```

If you want to see the exact output of the&nbsp;`render`&nbsp;method, call the&nbsp;`render_to_string`&nbsp;method. It uses the same options as the&nbsp;`render`&nbsp;method but returns the string result without sending response back to the server.

## Render a Template from Another Controller

To render a view template from another controller, you can specify the full path to the template using the&nbsp;`render`&nbsp;method. For example:

```ruby
# app/controllers/orders_controller.rb

class OrdersController
  def show_summary
    @order = Order.find(params[:id])
    render template: "invoices/summary"
  end
end
```

This will render the&nbsp;`app/views/invoices/summary.html.erb`&nbsp;template, even though the action is in&nbsp;`OrdersController`. The path is relative to&nbsp;`app/views`, without the file extension.

If you want, you can also specify a full path to a template in a different controller:

```ruby
render "admin/dashboard/show"
```

Rails will look for&nbsp;`app/views/admin/dashboard/show.html.erb`.

## Rendering Text, HTML, JSON, and XML

When building APIs or responding with simple messages, you often need to render raw content. You can do this using the&nbsp;`plain`,&nbsp;`html`,&nbsp;`json`, or&nbsp;`xml`&nbsp;options to the&nbsp;`render`&nbsp;method.

**Text**

```ruby
render plain: "Not authorized", status: :unauthorized
```

When using the&nbsp;`:plain`&nbsp;option, the text is rendered without the layout. If you want to use the layout, add the&nbsp;`layout: true`&nbsp;option and create the layout file&nbsp;`.text.erb`&nbsp;extension.

**HTML**

```ruby
render html: "<strong>Saved!</strong>".html_safe
```

**JSON**

```ruby
render json: @user
```

Note, the argument (@user above) must respond to&nbsp;`to_json`. The&nbsp;`render`&nbsp;method automatically calls&nbsp;`to_json`&nbsp;for you.

Or, customize the output:

```ruby
render json: { id: @user.id, name: @user.name }
```

If you want to explore the Rails internals to understand how it really works, check out this post.

[Understanding the Rails Rendering Process](/rails-rendering-process)

**XML**

```ruby
render xml: @user
```

Same with JSON, the&nbsp;`render`&nbsp;method will call&nbsp;`to_xml`&nbsp;for you.

## Rendering Raw File or Binary Data

If you want to send a file to the client (such as a static page), Rails can render a raw file from an absolute path.

```ruby
render file: Rails.root.join('public/terms.html')
```

Make sure you don't use user-provided inputs otherwise it can be used to access sensitive files in your app.

By default, this renders as HTML. If you want to serve it as text:

```ruby
render file: Rails.root.join('public/data.csv'), content_type: 'text/csv'
```

### Binary content
```ruby
send_data image_data, type: 'image/png', disposition: 'inline'
```

Or for file downloads:

```ruby
send_file '/path/to/file.pdf', type: 'application/pdf', disposition: 'attachment'
```

Technically not&nbsp;`render`, but still part of controller response APIs. If you don't need to provide the layout,&nbsp;`send_file`&nbsp;is often a faster and better option.

### Rendering Nothing

Sometimes, you don't want to render any body, but just want to send a header. In this case, you can use the&nbsp;`head`&nbsp;method, passing the HTTP status code.

```ruby
head :ok

# OR

head :unauthorized
```

## Options Available to Render

Here’s a summary of the common options you can pass to&nbsp;`render`:

**Rendering mode**

- **`:partial`**&nbsp;– Renders a partial.
- **`:inline`**&nbsp;– Renders an inline string of ERB.
- **`:file`**&nbsp;– Renders a file from disk.
- **`:body`**&nbsp;– Returns a raw string as the response body.
- **`:plain`**&nbsp;– Renders plain text.
- **`:html`**&nbsp;– Renders raw HTML (you must mark it&nbsp;`html_safe`).
- **`:json`**&nbsp;– Renders JSON (calls&nbsp;`to_json`&nbsp;on object).
- **`:xml`**&nbsp;– Renders XML (calls&nbsp;`to_xml`).
- **`:js`**&nbsp;– Renders JavaScript.

**Options**

- **`:content_type`**&nbsp;– Overrides the MIME type.
- **`:assigns`**&nbsp;– Provides the hash of instance variable assignments for the template.
- **`:locals`**&nbsp;– Provides the hash of local variable assignments for the template.
- **`:layout`**&nbsp;- Specify a different layout or render without a layout with&nbsp;`layout: false`.
- **`:location`**&nbsp;– Sets the&nbsp;`Location`&nbsp;header. Often used with&nbsp;`status: :found`.
- **`:status`**&nbsp;– Sets the HTTP status code (e.g.&nbsp;`:ok`,&nbsp;`:not_found`).
- **`:template`**&nbsp;– Renders a specific template file.
- **`:action`**&nbsp;– Renders a different action’s view in the same controller.
- **`:formats`**&nbsp;– Change the default format specified in the request (`:html`&nbsp;by default)
- **`:variants`**&nbsp;– Look for template variations of the same format.

Before rendering, Rails looks at the action name, locale, format, variant, etc. to figure out exactly what to render. If a template doesn't exist, or is not in the correct format or variant, Rails will raise the&nbsp;`ActionController::UnknownFormat`&nbsp;error and render&nbsp;`204 No Content`.

You don’t need to use all these variations daily, but knowing they exist gives you tools for every situation.

## Render to String

Sometimes you want to use your view rendering engine to generate a string, without actually sending it as a response. That’s what&nbsp;`render_to_string`&nbsp;is for.

Common use cases include:

- Sending HTML or text emails using the same templates as your web views.
- Generating a downloadable PDF from a view.
- Logging or caching the rendered output.

```ruby
html = render_to_string(template: "invoices/show", layout: false)
PdfService.generate(html)
```

Keep in mind that&nbsp;`render_to_string`&nbsp;still runs the full view rendering stack (layouts, helpers, partials), so it’s not free performance-wise, but it's often the cleanest way to reuse your existing views.

## Rendering Partials with Collections

Rendering collections is a performance-friendly way to output many items using a single partial. If you’re not using it yet, you should.

```ruby
<%= render partial: "posts/post", collection: @posts %>
```

Rails will call&nbsp;`_post.html.erb`&nbsp;for each item in&nbsp;`@posts`, assigning each one to a&nbsp;`post`&nbsp;local.

