Follow Along as I Stumble on the Path to Learning RoR

Detecting a Mobile Browser

I have some CSS elements that won’t show correctly in mobile browsers, so I needed to exclude those elements from showing if it was a mobile browser.

I ended up using this code written by Ryan Bates’ on Railscasts

You can add this code to your application_controller.rb:

helper_method :mobile_device?

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

Then you can add this to where ever you do the check:

<% if mobile_device? %>
your code
<% end %>

For added flexibility, there are also some gems available that will check specifically for what browser it is.
Browser gem


Comments are closed.