【问题标题】:Rails 3: HTTP_USER_AGENTRails 3:HTTP_USER_AGENT
【发布时间】:2011-07-10 18:17:12
【问题描述】:

我正在努力在网站上检测用户代理以获得技术支持,这样用户就不需要自己找到它。这是我到目前为止所得到的,但是无论我在什么浏览器上测试它都会显示为默认的“未知”关于我出错的地方有什么建议吗?

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

【问题讨论】:

  • mybrowser.scan(/MSIE (\d.\d*)/).first 将返回 IE 的版本号,如果他们不使用 IE,则返回 nil。只是想我会添加。

标签: ruby-on-rails-3 user-agent


【解决方案1】:

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

【讨论】:

  • 或者只是request.user_agent
【解决方案2】:

另外,查看 UserAgent gem:

https://github.com/gshutler/useragent

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2023-04-11
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多