Detecting Mobile Browser via Gem

Step 1: Install the gem ‘browser’

gem install browser

Step 2: Add it to Gemfile or environment.rb

gem "browser"         #=> Rails 3
config.gem "browser"  #=> Rails 2

Step 3: Implement

require "browser"
browser.name        # readable browser name
browser.safari?
browser.opera?
browser.chrome?
browser.mobile?
browser.tablet?
browser.firefox?
browser.ie?
browser.ie6?        # this goes up to 9
browser.capable?    # supports some CSS 3
browser.platform    # return :mac, :windows, :linux or :other
browser.mac?
browser.windows?
browser.linux?
browser.blackberry?
browser.meta        # an array with several attributes
browser.to_s        # the meta info joined by space

Example:-

<% if browser.ie6? %>
  <p>You're running an older IE version. Please update it!</p>
<% end %>
Advertisements