Rails Browser Detection Methods

34,284

Solution 1

There's library ruby library over at GitHub: https://github.com/gshutler/useragent

I use it myself, and it's working as advertised so far. For your use case, you could probably call the library from within a helper method in your Rails project or something similar.

That said, I'm not completely sure if the HTTP_USER_AGENT is exposed to Rails helper methods. In case it isn't exposed, you could always expose a controller method as a helper (by using AbstractController::Helpers::ClassMethods#helper_method).

Solution 2

The browser gem is specifically designed for browser detection in Rails.

Solution 3

Try request.env['HTTP_USER_AGENT'], this will return your client's User Agent. There's also a quick helper posted by Hubert Łępicki

Share:
34,284
alvincrespo
Author by

alvincrespo

I |> build software |> develop teams |> educate.

Updated on July 09, 2022

Comments

  • alvincrespo
    alvincrespo almost 2 years

    Hey Everyone, I was wondering what methods are standard within the industry to do browser detection in Rails? Is there a gem, library or sample code somewhere that can help determine the browser and apply a class or id to the body element of the (X)HTML? Thanks, I'm just wondering what everyone uses and whether there is accepted method of doing this?

    I know that we can get the user.agent and parse that string, but I'm not sure if that is that is an acceptable way to do browser detection.

    Also, I'm not trying to debate feature detection here, I've read multiple answers for that on StackOverflow, all I'm asking for is what you guys have done.

    [UPDATE]

    So thanks to faunzy on GitHub, I've sort of understand a bit about checking the user agent in Rails, but still not sure if this is the best way to go about it in Rails 3. But here is what I've gotten so far:

    def users_browser
    user_agent =  request.env['HTTP_USER_AGENT'].downcase 
    @users_browser ||= begin
      if user_agent.index('msie') && !user_agent.index('opera') && !user_agent.index('webtv')
                    'ie'+user_agent[user_agent.index('msie')+5].chr
        elsif user_agent.index('gecko/')
            'gecko'
        elsif user_agent.index('opera')
            'opera'
        elsif user_agent.index('konqueror')
            'konqueror'
        elsif user_agent.index('ipod')
            'ipod'
        elsif user_agent.index('ipad')
            'ipad'
        elsif user_agent.index('iphone')
            'iphone'
        elsif user_agent.index('chrome/')
            'chrome'
        elsif user_agent.index('applewebkit/')
            'safari'
        elsif user_agent.index('googlebot/')
            'googlebot'
        elsif user_agent.index('msnbot')
            'msnbot'
        elsif user_agent.index('yahoo! slurp')
            'yahoobot'
        #Everything thinks it's mozilla, so this goes last
        elsif user_agent.index('mozilla/')
            'gecko'
        else
            'unknown'
        end
        end
    
        return @users_browser
    end
    
  • alvincrespo
    alvincrespo over 13 years
    I tried HTTP_REFERER but that didn't return anything so I used HTTP_USER_AGENT and it returned the correct user agent, however I'm looking for a particular methods people have used to parse this string, for example like a custom built regex or library that exists that does this for you, sort of like a helper.
  • xinit
    xinit over 13 years
    The referrer is not related to the user agent. This is where a resource referred from (e.g. what url loaded an image or what other page loaded the page, etc)
  • alvincrespo
    alvincrespo over 13 years
    Thanks for the reference, I've used that library to create my own.
  • Christoph Schiessl
    Christoph Schiessl about 13 years
    I'm curious... what did you add in your version of the library? Is the source code available somewhere?
  • Andrew Burns
    Andrew Burns about 12 years
    Didn't know about the gem it it helped me greatly!
  • Evan
    Evan almost 11 years
    for tablets it only knows ipad and android as of today
  • rcd
    rcd about 10 years
    We have used this extensively in big data apps; it is very good. You can even modify it a bit to screen out bots; I believe I made a pull request with the ability to do so.
  • robd
    robd about 10 years
    Note: Ruby 1.9+ only AFAICT
  • gangothri
    gangothri over 3 years
    browser works only for ruby version >= 2.5.0
  • Sean Mitchell
    Sean Mitchell almost 3 years
    This helped me heaps, I appreciate the answer. Now all my stats only increment when the browser.bot? isn't true.