Rails 浏览器检测方法 [英] Rails Browser Detection Methods

查看:60
本文介绍了Rails 浏览器检测方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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.

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

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.

[更新]

感谢 GitHub 上的 faunzy,我有点明白了关于检查 Rails 中的用户代理,但仍然不确定这是否是 Rails 3 中的最佳方法.但这是我到目前为止所得到的:

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

推荐答案

GitHub 上有 ruby​​ 库:https://github.com/gshutler/useragent

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

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

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.

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

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).

这篇关于Rails 浏览器检测方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆