Rails way to detect mobile device? Rails way to detect mobile device? ruby ruby

Rails way to detect mobile device?


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/  endend

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.


In application_helper.rb:

def device  agent = request.user_agent  return "tablet" if agent =~ /(tablet|ipad)|(android(?!.*mobile))/i  return "mobile" if agent =~ /Mobile/  return "desktop"end

Then you can use it in your views:

<% if device == "mobile" %>  Show something for mobile users.<% end %>