Ruby-on-rails Rails 浏览器检测方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4707400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:07:31  来源:igfitidea点击:

Rails Browser Detection Methods

ruby-on-railsrubyuser-agentbrowser-detection

提问by alvincrespo

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?

大家好,我想知道业界在 Rails 中进行浏览器检测的标准方法是什么?是否有可以帮助确定浏览器并将类或 id 应用于 (X)HTML 的 body 元素的 gem、库或示例代码?谢谢,我只是想知道每个人都使用什么以及是否有公认的方法?

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.

我知道我们可以获取 user.agent 并解析该字符串,但我不确定这是否是进行浏览器检测的可接受方式。

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.

另外,我不是想在这里讨论功能检测,我已经在 StackOverflow 上阅读了多个答案,我所要求的只是你们所做的。

[UPDATE]

[更新]

So thanks to faunzyon 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:

多亏了GitHub 上的faunzy,我对在 Rails 中检查用户代理有了一些了解,但仍然不确定这是否是 Rails 3 中最好的方法。但这是我得到的远的:

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

采纳答案by Christoph Schiessl

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

GitHub 上有库 ruby​​ 库: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.

我自己使用它,到目前为止它的工作原理与宣传的一样。对于您的用例,您可能可以从 Rails 项目中的辅助方法或类似方法中调用该库。

That said, I'm not completely sure if the HTTP_USER_AGENTis 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).

也就是说,我不完全确定 是否HTTP_USER_AGENT暴露于 Rails 辅助方法。如果它没有公开,您可以始终将控制器方法公开为帮助程序(通过使用AbstractController::Helpers::ClassMethods#helper_method)。

回答by Rob

The browsergem is specifically designed for browser detection in Rails.

浏览器的宝石是专为在Rails的浏览器检测而设计的。

回答by farnoy

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

尝试request.env['HTTP_USER_AGENT'],这将返回您客户的用户代理。Hubert ??picki也发布了一个快速助手