2011-03-08 19 views
17

我正在检测用户代理在网站上的技术支持,以便用户无需自己查找。这是我到目前为止,但无论我测试它在哪个浏览器上显示为默认“未知”任何建议我出错的地方?Rails 3:HTTP_USER_AGENT

def cyberbrowser 
    mybrowser = ENV["HTTP_USER_AGENT"] 
     case mybrowser 
     when /MSIE 8.0/ then "Internet Explorer V8" 
     when /MSIE 7.0/ then "Internet Explorer V7" 
     when /MSIE 6.0/ then "Internet Explorer V6.0+" 
     when /MSIE 5.5/ then "Internet Explorer V5.5" 
     when /MSIE 5.22/ then "Internet Explorer V5.22" 
     when /MSIE 5.0/ then "Internet Explorer V5.0+" 
     when /MSIE 4.0/ then "Internet Explorer V4.0+" 
     when /MSIE 3.0/ then "Internet Explorer V3.0+" 
     when /MSIE 2.0/ then "Internet Explorer V2.0+" 
     when /Firefox/ then "Mozilla Firefox" 
     when /Camino/ then "Camino" 
     when /Dillo/ then "Dillo" 
     when /Epiphany/ then "Epiphany" 
     when /Firebird/ then "Mozilla Firebird" 
     when /Thunderbird/ then "Mozilla Thunderbird" 
     when /Galeon/ then "Mozilla Galeon" 
     when /IBrowse/ then "IBrowse" 
     when /iCab/ then "iCab" 
     when /K-Meleon/ then "K-Meleon" 
     when /Konqueror/ then "Konqueror" 
     when /SeaMonkey/ then "SeaMonkey" 
     when /Netscape/ then "Netscape" 
     when /OmniWeb/ then "OmniWeb" 
     when /Opera/ then "Opera" 
     when /Safari/ then "Safari" 
     else "Unknown" 
     end 
end 
+0

'mybrowser.scan first'将返回IE的版本号,或者'为零。 '如果他们不使用IE。只是想我会补充一点。 – Zaz 2013-05-16 12:19:11

回答

50

在您的控制器中尝试request.env['HTTP_USER_AGENT']。如果这在您的模型中,请将其传递给您的模型。

+4

或者只是'request.user_agent' – 2016-05-24 14:39:14

4

另外,检出的UserAgent宝石:(/ MSIE(\ d \ d *)/)

https://github.com/josh/useragent

+0

该项目不再存在。 – 2016-07-24 16:39:57

+0

@ChrisCirefice这一个工程:https://github.com/gshutler/useragent – 2016-08-29 00:16:17