Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails way to detect mobile device?

Is there a "rails way" to detect whether the user is using a mobile device? What I mean is a method I can use in erb, something like this:

<% if mobile? %>
like image 741
Joe Morano Avatar asked Mar 16 '15 02:03

Joe Morano


People also ask

What is the best way to detect a mobile device?

In summary, we recommend looking for the string “Mobi” anywhere in the User Agent to detect a mobile device. Like this: if (/Mobi/. test(navigator.

What is the best way to detect a mobile device JavaScript?

matchMedia() The Window. matchMedia() is one of the best properties for detecting mobile users with JavaScript.


2 Answers

You can do it that way by defining a function like below:

def mobile_device?   if session[:mobile_param]     session[:mobile_param] == "1"   else     request.user_agent =~ /Mobile|webOS/   end end 

Or you can use gems to detect mobile devices like

  • https://github.com/tscolari/mobylette
  • https://github.com/shenoudab/active_device
like image 111
utnas Avatar answered Oct 02 '22 11:10

utnas


Use browser gem. You are able to detect the browser by the custom user_agent.

like image 29
askl56 Avatar answered Oct 02 '22 09:10

askl56