Let’s filter out some unwanted browsers from displaying broken pages in our Rails application. We could disallow IE6, for example, but allow IE7, 8 and 9.
Bring in the useragent gem – we’ll need to take it from a popular jillion fork – it has a ton of fixes that became too hard to merge back to its parent. The latter has many bugs in version comparisons to make it useful for our purposes as of the time of writing this.
We can add an app_initialization _to _ApplicationController that all controllers derive from which will render an error page if the browser is not supported.
The template can be a page placed in app/views/errors/browser.html.haml.
Finally, declare an array of supported and an array of unsupported browsers within ApplicationController. A supported browser is a structure of browser name and version. An unsupported browser name is sufficient. We first check whether the browser is explicitly unsupported, then whether the version of a supported browser is bigger or equal to a specific one.
Finally, we let everyone with an unknown browser through – we can’t possibly whitelist hundreds of search engine user-agents.