---
title: "Order of format matters in respond_to block"
description:
  "In Rails we use respond_to to respond to different types of requests. The
  order in which different formats are handled matters. This blog takes a look
  at that issue."
canonical_url: "https://www.bigbinary.com/blog/order-of-format-matters-in-respond_to-block"
markdown_url: "https://www.bigbinary.com/blog/order-of-format-matters-in-respond_to-block.md"
---

# Order of format matters in respond_to block

In Rails we use respond_to to respond to different types of requests. The order
in which different formats are handled matters. This blog takes a look at that
issue.

- Author: Neeraj Singh
- Published: January 25, 2010
- Categories: Rails

This is a standard Rails code. I am using Rails 2.3.5 .

```ruby
class UsersController < ApplicationController
  def index
    @users = User.all
    respond_to do |format|
      format.html
      format.js  { render :json => @users }
    end
  end
end
```

Accidentally in one of my controllers the order of formats got reversed. The
altered code looks like this.

```ruby
class UsersController < ApplicationController
  def index
    @users = User.all
    respond_to do |format|
      format.js  { render :json => @users }
      format.html
    end
  end
end
```

I thought order of format declaration does not matter. I was wrong.

```plaintext
> curl -I http://localhost:3000/users
HTTP/1.1 200 OK
Connection: close
Date: Mon, 25 Jan 2010 22:32:16 GMT
ETag: "d751713988987e9331980363e24189ce"
Content-Type: text/javascript; charset=utf-8
X-Runtime: 62
Content-Length: 2
Cache-Control: private, max-age=0, must-revalidate
```

Notice the Content-Type of in the response header is

<strong>text/javascript</strong> in stead of <strong>text/html</strong> .

Well I guess the order of format matters. I hope it is fixed in Rails 3.

## Links

- [Human page](https://www.bigbinary.com/blog/order-of-format-matters-in-respond_to-block)
