# Browser detection with Rails?

**URL:** <https://www.ruby-forum.com/t/browser-detection-with-rails/145287>\
**Category:** Rails\
**Created:** [August 7, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/browser-detection-with-rails/145287 "2008-08-07T14:57:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [August 7, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/browser-detection-with-rails/145287/1 "2008-08-07T14:57:03Z")

</div>

hi

I can’t find any posibility to detect the browser with rails so i tried  
javascript.

* * *

## 

The problem is, that the flash message will shown independent of the IF  
request.

Any ideas?

Greeting Chaos

---

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [August 7, 2008, 5:09pm UTC](https://www.ruby-forum.com/t/browser-detection-with-rails/145287/2 "2008-08-07T17:09:31Z")

</div>

Andi G. wrote:

> hi
> 
> I can’t find any posibility to detect the browser with rails so i tried  
> javascript.
> 
> Any ideas?
> 
> Greeting Chaos

Did you try using request.env[‘HTTP\_REFERER’]? I think this can be used  
to detect the browser type. Not sure what all the info that this  
returned but I think browser type was one of them.

-S

---

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [August 7, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/browser-detection-with-rails/145287/3 "2008-08-07T17:33:29Z")

</div>

I once used something like this:

```
             def client_browser_name
                      user_agent =

```

request.env[‘HTTP\_USER\_AGENT’].downcase  
if user\_agent =~ /msie/i  
“Internet Explorer”  
elsif user\_agent =~ /konqueror/i  
“Konqueror”  
elsif user\_agent =~ /gecko/i  
“Mozilla”  
elsif user\_agent =~ /opera/i  
“Opera”  
elsif user\_agent =~ /applewebkit/i  
“Safari”  
else  
“Unknown”  
end  
end

On Aug 7, 5:09 pm, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [August 7, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/browser-detection-with-rails/145287/4 "2008-08-07T20:24:47Z")

</div>

Thanks a lot to both! The solution works great!

cya chaos

---

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [August 8, 2008, 10:55am UTC](https://www.ruby-forum.com/t/browser-detection-with-rails/145287/5 "2008-08-08T10:55:59Z")

</div>

Walter Lockhart wrote:

> Hi Andi,
> 
> where in your Rails application did you place this method? And from  
> where did you call this method? In other words, how did you use this  
> code?
> 
> Thanks in advance.
> 
> Walter
> 
> On Aug 7, 7:24ï¿½pm, Andi G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hi Walter,

I placed the method in the application\_helper.rb in the app/helpers  
directory.

Then I call the method in the application layer in  
app/views/layer/application.rb with \<% client\_browser\_name %\>. But you  
can call this method also in every other view.

Ask if you have still questions.

Greetings Andi

---

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [August 8, 2008, 10:31am UTC](https://www.ruby-forum.com/t/browser-detection-with-rails/145287/6 "2008-08-08T10:31:12Z")

</div>

Hi Andi,

where in your Rails application did you place this method? And from  
where did you call this method? In other words, how did you use this  
code?

Thanks in advance.

Walter

On Aug 7, 7:24 pm, Andi G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [March 27, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/browser-detection-with-rails/145287/7 "2009-03-27T14:28:49Z")

</div>

One typo above:

```
isSafari3 = (ua.index('webkit/5')) ? true : false
```

---

<div class="post-metadata">

**Author:** ![chaosbanane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@chaosbanane](https://www.ruby-forum.com/u/chaosbanane)\
**Post date:** [August 31, 2008, 5:40am UTC](https://www.ruby-forum.com/t/browser-detection-with-rails/145287/8 "2008-08-31T05:40:19Z")

</div>

Hi,

I am using this code based on the javascript browser detection code in  
the open source ExtJS framework ([ExtJS.com](http://ExtJS.com)) and converted to Rails:

def getBrowser(bt)  
rs=false  
ua=request.env[‘HTTP\_USER\_AGENT’].downcase  
isOpera = ua.index(‘opera’) ? true : false  
isSafari = (ua =~ /webkit|khtml/) ? true : false  
isSafari3 = (ua.index(‘webkit/5’) ? true : false  
isGecko = (!isSafari and ua.index(‘gecko’)) ? true : false  
isGecko3 = (!isSafari and ua.index(‘rv:1.9’)) ? true : false  
isIE = (!isOpera and ua.index(‘msie’)) ? true : false  
isIE7 = (!isOpera and ua.index(‘msie 7’)) ? true : false  
case bt  
when 0 #isKonqueror  
if ua.index(‘konqueror’) then rs=true end  
when 1 #isOpera  
rs=isOpera  
when 2 #isSafari  
rs=isSafari  
when 3 #isSafari2  
rs=isSafari && !isSafari3  
when 4 #isSafari3  
rs=isSafari3  
when 5 #isIE  
rs=isIE  
when 6 #isIE6  
rs=isIE && !isIE7  
when 7 #isIE7  
rs=isIE7  
when 8 #isGecko  
rs=isGecko  
when 9 #isGecko2  
rs=isGecko && !isGecko3  
when 10 #isGecko3  
rs=isGecko3  
when 11 #isWindows  
if ua.index(‘windows’) or ua.index(‘win32’) then rs=true end  
when 12 #isMac  
if ua.index(‘macintosh’) or ua.index(‘mac os x’) then rs=true  
end  
when 13 #isAir  
if ua.index(‘adobeair’) then rs=true end  
when 14 #isLinux  
if ua.index(‘linux’) then rs=true end  
when 15 #isSecure  
s = request.env[‘SERVER\_PROTOCOL’].downcase  
if s.index(‘https’) then rs=true end  
end  
rs  
end
