# How respond-to Method Works in Rails

*Published on December 07, 2023*

Tags: Rails Internals

---


So, I was messing around with a Rails app yesterday, doing the usual `respond_to` method thing. Then it hit me that I barely know squat about this method, except that it sends responses in different formats, depending on the request's MIME type. I ended up spending a couple of hours diving into the Rails API docs, guides, and the Rails source code. This post sums up everything I've learned so far.

**My biggest takeaway?** The number of hoops Rails jumps through to make the final DSL (or API) convenient for the end-users (Rails developers) is mind-boggling. Much respect.

---

In the previous post on the Rails Router, we learned the concepts of&nbsp;[resources and resourceful routing](/understanding-rails-router-why-what-how#resourceful-routes). In its essence, a resource is a key abstraction of information and any information that can be named can be a resource. A single resource can have different representations, also called formats.

For example, consider the Post resource. We could represent a post as an HTML page or as a JSON document.

![format.png](https://writesoftwarewell.com/media/h7561qq24xsmgi6d8pc0hdrrq53m.png "HTML vs. JSON")

Now you might be thinking, like, who gets to pick the format in which the response is sent?

The answer is: since HTTP is a client-server protocol, the client (browser) and the server (backend-application) together negotiate the response format. The client tells the server that it's looking for a specific representation, and the server responds with that format.

**Let's see how this works in practice, in the context of a Rails application.**

When the browser sends an HTTP request to your Rails application, it provides a list of formats that it can understand and interpret. For this, the browser uses the&nbsp;`Accept`&nbsp;HTTP header.

For example, to indicate that it will accept only HTML responses, the browser can set the&nbsp;`Accept`&nbsp;header to&nbsp;`text/html`&nbsp;([check out the common MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types)).

```text
// accept HTML
Accept: text/html

// OR, accept images
Accept: image/*

// OR, accept anything
Accept: */*
```

The browser can also pass multiple formats.

```text
Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*
```

Upon receiving the request from the browser, the server (Rails app) then chooses a format from the provided list or declines the request.

> 💡 The&nbsp;**`Accept`**&nbsp;request HTTP header indicates which content types (also known as&nbsp;[MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)) the client is able to understand. The server uses&nbsp;[content negotiation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation)&nbsp;to select one of the proposals and informs the client of the choice with the&nbsp;[`Content-Type`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type)&nbsp;response header.&nbsp;[_Source_](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept)

Specifically, this process of negotiating the response format is called content negotiation.&nbsp;**Content negotiation&nbsp;determines how a specific representation is selected when the client requests a resource.**

1. When a client wants to obtain a resource, the client requests it via a URL, passing the format via the&nbsp;`Accept`&nbsp;header or the URL extension (see below).
2. The server uses this URL to choose one of the variants available–each variant is called a&nbsp;_representation_–and returns a specific representation to the client.

> 💡 Although I'm using the term 'browser' for the client, it doesn't have to be. You can use&nbsp;`curl`&nbsp;or any other program to make HTTP requests and pass the&nbsp;`Accept`&nbsp;HTTP header. In curl, you can set the request header with the&nbsp;`-H`&nbsp;option.

### **URL&nbsp;Format Recognition in Rails**

Every route you create in your Rails application automatically recognizes the format for URLs that end with a dot (`.`) and the format parameter, which is optional. That's why, when you print routes using&nbsp;`bin/rails routes`&nbsp;command, you see&nbsp;`(.:format)`&nbsp;at the end of each URI pattern.

```text
   Prefix Verb   URI Pattern               Controller#Action
    posts GET    /posts(.:format)          posts#index
          POST   /posts(.:format)          posts#create
```

For example, if the client requests the URL&nbsp;`yourapp.com/posts/first-post.json`, the server treats it in the same way as if the browser had set the&nbsp;`Accept`&nbsp;header to&nbsp;`application/json`.

```text
$ curl -v http://yourapp.com/posts/first-post.json

GET /posts/first-post
Host: yourapp.com
Accept: */*
```

Since you didn't specify the&nbsp;`Accept`&nbsp;header explicitly, curl used \* / \* , indicating that it accepts any MIME type.

So far, we've only seen how the client sends the format types it needs. We don't know how the your Rails application (server) handles the request to send the response in the correct format. Let's explore that now.

## How the respond\_to Method Works

The Rails controller accepts the incoming HTTP requests and returns the format requested by the client. The&nbsp;`respond_to`&nbsp;method provides an elegant DSL inside the controller actions, allowing you to return different results based on the format requested by the client.

For example, consider the&nbsp;`ReportsController#show`&nbsp;action which returns the report either as an HTML page or a PDF or JSON document, depending on the requested format.

```ruby
class ReportsController < ApplicationController

  def show
    @report = Report.find(params[:id])

    respond_to do |format|
      format.html
      format.pdf
      format.json { render json: @report.to_json }
    end
  end
end
```

The first time I saw the above code in Rails, my procedural brain, coming from C# and .NET, couldn't comprehend what was happening. My first impression was we were first returning an HTML response, then a PDF, and finally, the JSON.

**Why send three responses for one request? 🤔**

I couldn't be more wrong. This is what the above code is saying, instead:

💡 If the browser wants to see the report HTML in the browser, render the `reports/show.html.erb` template as usual. If they want the report as a PDF, render the corresponding PDF template. However, if they want JSON, return the report data as a JSON document.

So, in the&nbsp;`respond_to`&nbsp;block, we are only configuring what should happen when the client needs a particular MIME type. When the request comes, Rails automatically determines the desired response format from the HTTP Accept header submitted by the client, and sends the corresponding response format.

**What happens when the client requests a format that's not included in the** **`respond_to`** **block?**Rails simply returns a 406 Not Acceptable status, to indicate that it can't handle the request.

The&nbsp;`respond_to`&nbsp;method&nbsp;also allows you to specify a common block for different formats by using&nbsp;`any`:

```ruby
def show
  @product = Product.first

  respond_to do |format|
    format.html
    format.any(:xml, :json) { ... }
  end
end
```

With that understanding, let's try to briefly understand how the&nbsp;`respond_to`&nbsp;works.

First things first. The&nbsp;`respond_to`&nbsp;is just a method that accepts a block that is used to define responses to different mime-types.

```ruby
respond_to do |format|
  format.html
  format.xml { render xml: @people }
end
```

In this example, the argument passed to the block, i.e.&nbsp;`format`&nbsp;is an instance of the&nbsp;[`ActionController::MimeResponds::Collector`](https://api.rubyonrails.org/classes/ActionController/MimeResponds/Collector.html)&nbsp;class. This class acts as a container for responses available from the current controller for requests for different mime-types&nbsp;(formats). However, if you open the&nbsp;`Collector`&nbsp;class, you don't see any of those methods in there.

What gives? 🤔

> 💡 The mime-type-specific methods like&nbsp;`html`,&nbsp;`xml`&nbsp;are dynamically generated on the fly, using Ruby's&nbsp;**`method_missing`**&nbsp;method.

The&nbsp;`Collector`&nbsp;class uses its&nbsp;`method_missing`&nbsp;to dynamically register the methods ([this is where it's defined](https://github.com/rails/rails/blob/c057edaaadbec0cd1f22cbfac9697e8e0b51f73e/actionpack/lib/abstract_controller/collector.rb#L26)).

- When you call the&nbsp;`html`&nbsp;method without any arguments or block, it instructs Rails to handle routine HTML requests using regular views, i.e. templates, and layouts.
- When we call&nbsp;`xml`, we are telling it to respond to requests with the&nbsp;`.xml`&nbsp;extension by serializing to XML format.

There are two ways to call&nbsp;`respond_to`: Either pass&nbsp;a list of accepted MIME types or pass a block (as shown above), but&nbsp;**you can't pass them both.**&nbsp;The first version is recommended when you’re not doing anything special to render your resources but still want to support multiple MIME-types.

```ruby
def index
  @people = Person.all
  respond_to :html, :js
end

# OR

def index
  @people = Person.all

  respond_to do |format|
    format.html
    format.js
    format.xml { render xml: @people }
  end
end
```

### Declare Custom MIME Types

If you need to use a MIME type which isn’t supported by default, you can register your own handlers in&nbsp;`config/initializers/mime_types.rb`&nbsp;as follows.

```ruby
Mime::Type.register "image/jpeg", :jpg
```

For example, the&nbsp;`turbo-rails`&nbsp;gem&nbsp;[defines a custom MIME type](https://github.com/hotwired/turbo-rails/blob/4eb4e928e30be8cd537af8073f98b80ddea4a578/lib/turbo/engine.rb#L62)&nbsp;called&nbsp;`text/vnd.turbo-stream.html`&nbsp;to handle Turbo Stream responses.

```ruby
# lib/turbo/engine.rb

initializer "turbo.mimetype" do
  Mime::Type.register "text/vnd.turbo-stream.html", :turbo_stream
end
```

Then you can use it in your controllers as follows:

```ruby
def destroy
  @message = Message.find(params[:id])
  @message.destroy

  respond_to do |format|
    format.turbo_stream { render turbo_stream: turbo_stream.remove(@message) }
    format.html         { redirect_to messages_url }
  end
end
```

### Custom Variants for a Format

Sometimes, you may want to render different templates for phones, tablets, and desktop browsers. For this, Rails supports format variants. Each format can have different variants, i.e. a specialization of the request format, like&nbsp;`:tablet`,&nbsp;`:phone`, or&nbsp;`:desktop`.

You can set the variant in a&nbsp;`before_action`:

```ruby
request.variant = :tablet if /iPad/.match?(request.user_agent)
```

Respond to variants in the action just like you respond to formats:

```ruby
respond_to do |format|
  format.html do |variant|
    variant.tablet # renders app/views/projects/show.html+tablet.erb
    variant.phone { extra_setup; render ... }
    variant.none  { special_setup } # executed only if there is no variant set
  end
end
```

Provide separate templates for each format and variant:

```text
app/views/projects/show.html.erb
app/views/projects/show.html+tablet.erb
app/views/projects/show.html+phone.erb
```

For more details, check out the&nbsp;`respond_to`&nbsp;[documentation](https://api.rubyonrails.org/classes/ActionController/MimeResponds.html#method-i-respond_to).

