# Unexpected behavior with respond\_to

**URL:** <https://discuss.rubyonrails.org/t/unexpected-behavior-with-respond-to/53274>\
**Category:** rubyonrails-talk\
**Created:** [July 29, 2010, 9:14pm UTC](https://discuss.rubyonrails.org/t/unexpected-behavior-with-respond-to/53274 "2010-07-29T21:14:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erik\_Hetzner](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@Erik\_Hetzner](https://discuss.rubyonrails.org/u/Erik_Hetzner)\
**Post date:** [July 29, 2010, 9:14pm UTC](https://discuss.rubyonrails.org/t/unexpected-behavior-with-respond-to/53274/1 "2010-07-29T21:14:35Z")

</div>

Hi all,

I am encountering some unexpected behavior in the Rails 3.0.0.rc which I think may be incorrect.

In my controller, I have the following:

&nbsp;&nbsp;class FeedsController \< ApplicationController &nbsp;&nbsp;&nbsp;&nbsp;def recent &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.atom &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

Likewise, I have a view (feeds/recent.atom.builder):

&nbsp;&nbsp;xml.tag!('feed', :xmlns =\> "[http://www.w3.org/2005/Atom&quot;\](http://www.w3.org/2005/Atom&quot;%5C)) do &nbsp;&nbsp;&nbsp;&nbsp;xml.text!("...") &nbsp;&nbsp;end

and I have a route:

&nbsp;&nbsp;match('feeds/recent' =\> 'feeds#recent')

If I try to fetch this with no Accept header, I get a 406 error:

&nbsp;&nbsp;$ curl -i [http://localhost:3000/feeds/recent](http://localhost:3000/feeds/recent) -H "Accept:" &nbsp;&nbsp;HTTP/1.1 406 Not Acceptable &nbsp;&nbsp;[...]

This is probably not correct, since RFC 2616 (sec 14.1) reads:

> If no Accept header field is present, then it is assumed that the client accepts all media types.

Moving on.

If I try to fetch this with an "Accept: \*/\*" header or "Accept: application/atom+xml", it works as expected. Likewise all works well if I use an "Accept: \*/\*, text/html" header

However, if I change the order of the formats, to "Accept: text/html, \*/\*", I get a 406:

&nbsp;&nbsp;$ curl -i [http://localhost:3000/feeds/recent](http://localhost:3000/feeds/recent) -H "Accept: text/html, \*/\*" &nbsp;&nbsp;HTTP/1.1 406 Not Acceptable &nbsp;&nbsp;[...]

But a "text/html, \*/\*" Accept header should be equivalent to a "\*/\*, text/html" header, since order does not matter. (This is a contrived, but similar Accept headers are used by Firefox, with q values.)

Getting further into the bowels of RFC 2616, an "Accept: application/\*" header gives me a 406, when it should be acceptable, because I have a response of type "application/atom+xml".

One final thing. I add a format.any entry to the controller (I am not entirely sure the meaning of this):

&nbsp;&nbsp;def recent &nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.atom &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.any &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

And now I get a 200 with "Accept: \*/\*" and "Accept: application/atom+xml", but template errors with any other Accept header including a "\*/\*".

Does anybody have any thoughts about this? It does not seem to me that Rails is not doing the right thing here.

best, Erik Hetzner

---

<div class="post-metadata">

**Author:** ![Svilen\_Vassilev](https://avatars.discourse-cdn.com/v4/letter/s/9de0a6/32.png) [@Svilen\_Vassilev](https://discuss.rubyonrails.org/u/Svilen_Vassilev)\
**Post date:** [September 6, 2010, 1:05am UTC](https://discuss.rubyonrails.org/t/unexpected-behavior-with-respond-to/53274/2 "2010-09-06T01:05:17Z")

</div>

I have similar issues with some js.rjs files after upgrading an app to rails 3. Some respond\_to blocks keep working fine as before and I get JS returned, some keep trying to return HTML and thus spit out the 406 error. I haven’t found any good reason in my code to justify this kind of behavior, even after i tried reducing the affected js.rjs files to a simple straightforward single line of code: I still get them returned as html with error 406. I even tried to enforce javascript headers by putting: format.js {render :content\_type =\> ‘text/javascript’} in the respond\_to block, but that didn’t change it.

Really stuck on this issue. In 2.3.8 the code worked perfectly fine.
