Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to detect the user's operating system in Rails 3?

Simple Rails app, mostly scaffolding. I want to detect whether the user is using Android or iPhone to access my app. What's the easiest way to do that?

like image 400
Francis Potter Avatar asked Feb 26 '11 07:02

Francis Potter


People also ask

How to detect os?

To detect the operating system on the client machine, one can simply use navigator. appVersion or navigator. userAgent property. The Navigator appVersion property is a read-only property and it returns a string which represents the version information of the browser.

How to detect os in js?

Detect Operating System With the userAgent Property in JavaScript. If we want to track a little detail about the user's used browser and platform, we can go for the userAgent property with the navigator object. This special feature returns a string containing the browser's name, version, and platform name.


2 Answers

if request.env['HTTP_USER_AGENT'].downcase.match(/android|iphone/)
  puts "yup, its mobile"
end
like image 114
johnmcaliley Avatar answered Sep 27 '22 21:09

johnmcaliley


I know this question is old, but in case this helps someone else, I use this method in application_controller.rb to automatically set the format to :mobile:

before_filter :detect_mobile

protected

def detect_mobile
  request.format = :mobile if mobile?
end

def mobile?
  request.user_agent =~ /iPhone|iPad|Android/i
end

The mobile? method is separate so that you can also use it in your own controllers if you need to do some sort of conditional logic for mobile browsers.

like image 43
markquezada Avatar answered Sep 27 '22 19:09

markquezada