Bug of the day: here’s a generic error message in Rails + stack trace. For context, this occurs during an HTTP request coming from an iPhone:

undefined method `to_sym' for nil:NilClass
  /Library/Ruby/Gems/1.8/gems/activesupport-3.0.4/lib/active_support/whiny_nil.rb:48:in `method_missing'
  /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_dispatch/http/filter_parameters.rb:52:in `to_proc'
  /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:22:in `map'
  /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/instrumentation.rb:22:in `process_action'
  /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/rescue.rb:17:in `process_action'
  /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/base.rb:119:in `process'
  /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/abstract_controller/rendering.rb:41:in `process'
  /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal.rb:138:in `dispatch'
  /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_controller/metal.rb:178:in `action'
Rendered text template (0.0ms)

First, I pasted the URL into my browser, it worked as expected.

What could possibly be going wrong? After a bit of debugging, found out that request.format was nil!!??


First, I tried to come up with a before_filter to set the format:

:before_filter set_format
def set_format
  request.format ||= :html
end

Unfortunately, the error was occurring before any before_filters were getting fired. Next, I tried adding some MIME types in the initializer, no effect.

Finally, I stared at the problem some more and thought about what could be different between a browser request and my HTTP call from the phone – HTTP headers. I set one header, and everything worked like magic:

    this.xhr.setRequestHeader("Content-type", "text/html");

Amazing… all I needed to send was a content-type header, no more wacky errors. I’m not sure how I can prevent this on the server side without monkey-patching Rails… :)

For all you out there who’re hitting this error, now you know.