Rails 3:HTTP_USER_AGENT

问题描述 投票:18回答:2

我正在努力检测用户代理以获取站点上的技术支持,以使用户无需自己查找它。到目前为止,这就是我所得到的,但是无论我在哪种浏览器上对其进行测试,它都会显示为默认的“未知”。关于我哪里出问题的任何建议?

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
ruby-on-rails-3 user-agent
2个回答
51
投票

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


4
投票

另外,请检出UserAgent gem:

https://github.com/gshutler/useragent

© www.soinside.com 2019 - 2024. All rights reserved.