2011-03-08 14 views
17

मैं किसी साइट पर तकनीकी सहायता के लिए उपयोगकर्ता एजेंट का पता लगाने पर काम कर रहा हूं ताकि उपयोगकर्ता को इसे स्वयं ढूंढने की आवश्यकता न हो। यह वही है जो मुझे अब तक मिला है, लेकिन इससे कोई फर्क नहीं पड़ता कि मैं जिस ब्राउजर पर इसका परीक्षण करता हूं, वह डिफ़ॉल्ट "अज्ञात" के रूप में दिखाई देता है, जहां पर मैं गलत हो गया था?रेल 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 के संस्करण संख्या, या' शून्य वापस आ जाएगी। 'अगर वे आईई का उपयोग नहीं कर रहे हैं। बस सोचा कि मैं इसे जोड़ दूंगा। – Zaz

उत्तर

50

अपने नियंत्रक में request.env['HTTP_USER_AGENT'] आज़माएं। यदि यह आपके मॉडल में है, तो इसे अपने मॉडल पर पास करें।

+4

या बस 'request.user_agent' –

4

इसके अलावा, UserAgent मणि चेकआउट: (।/MSIE (\ घ \ d *) /)

https://github.com/josh/useragent

+0

वह प्रोजेक्ट अब मौजूद नहीं है। –

+0

@ChrisCirefice यह एक काम करता है: https://github.com/gshutler/useragent –