Rails Footnotes problem

I recently had a head-scratching time trying to figure out why code that integrates with a 3rd party payment processor had suddenly stopped working. The code in question simply returns a text only response to the 3rd party, who then read, parse and act on it. Within my Rails controller:

1
render :text => "Status=OK,Detail=Some details here"

I found the culprit by going through my git log to see what had changed recently. Turns out it was the recent addition of Rails Footnotes causing the problem. Rails Footnotes is an awesome gem that adds various information about your app to the bottom of every page including clickable links to controllers, views and partials. Clicking these links opens the file in your text editor of choice.

Rails Footnotes was appending its debugging HTML (as designed) to my text-only response, breaking the payment processors parsing. After an inspection of the source I noticed that Footnotes are only output if the content type includes “html”, such as “text/html”.

The fix

In my case the whole controller could (and probably should) be returned as “text/plain”. I hooked up a before_filter to add the Content-Type header to the response and that fixed it.

1
2
3
4
5
6
7
8
9
class NotificationController < ApplicationController # Set the content type to text/plain so footnotes don't show before_filter :set_content_type def set_content_type headers['Content-Type'] = 'text/plain' end end

For versions of Rails Footnotes greater than 3.7.0 (which is Rails 3 only) you can actually configure if Footnotes are added to the page using an initializer (or similar). Unfortunately, I’m still using Rails 2 on this project so I currently can’t take advantage of that.

In conclusion

Rails Footnotes is a must-have, you really should check it out.