Render :json not setting content type (Rails 3.0.3)


I’m experiencing a strange problem on an application this is in the
process of being converted from Rails 2.3 to 3.0.3. I have several
controllers that are rendering either HTML or JSON depending on the
requested format, using respond_to:

respond_to |wants|
wants.json {render :json => “blah”}

One controller works just fine. The other is sending the wrong
content type header - “text/html” instead of “application/json”. If I
remove the respond_to block in the second controller, and just
render :json all the time, then the correct type is sent. There are
no apparent differences between the working and non-working
controllers except the model being retrieved from the database. I’ve
tried reducing the actions down to rendering just a string as JSON (as
seen above) and still have the same problem.

Does anyone have a suggest as to what could be happening? How can I
trace the response back through the Rails stack to perhaps see where
the content type is being set (or not set?)

I can provide more detail if needed.



It took a little while longer, but I found a supposedly undocumented
gotcha in Rails 3 that was causing my problem.

The controller in question had a private method “content_type”
defined. Apparently that method is already defined for controllers,
and overriding it was causing the problem with the MIME type. So,
don’t define a content_type method in your controller.