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? %>
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.
matchMedia() The Window. matchMedia() is one of the best properties for detecting mobile users with JavaScript.
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
Use browser gem. You are able to detect the browser by the custom user_agent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With