Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Automatically detect mobile requests from mobile devices in your Rails application.
branch: master

This branch is even with billymeltdown:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


Mobile Fu (billymeltdown fork)

Want to automatically detect mobile devices that access your Rails application? Mobile Fu allows you to do just that. People can access your site from a Palm, Blackberry, iPhone, Nokia, etc. and it will automatically adjust the format of the request from :html to :mobile.

Modification Notes

I've modified this plugin a bit so that the use of the has_mobile_fu method simply includes the plugin, but doesn't automatically set any before filters.

I did this because I only want to run it on some actions, where I've actually taken the time to provide mobile views (e.g.

The other major changes:

By providing the parameter 'bypass' with any request and setting it to the string 'true', the end-user can switch back to :html format for the rest of his session, allowing me to create links to 'Exit mobile view', etc. To turn mobile viewing back on, bypass can be set to something else, like 'false', or 'superdisestablishmententarianism'.

Directions below have been modified to conform to these changes.


Add this to one of your controllers:

class ApplicationController < ActionController::Base
  before_filter :set_mobile_format

With this in place, any request that comes from a mobile device will be be set as :mobile format. It is up to you to determine how you want to handle these requests. It is also up to you to create the .mobile.erb versions of your views that are to be requested. You can use the standard options for before_filter to limit the scope of which requests are processed this way:

class SessionController < ApplicationController
  before_filter :set_mobile_format, :except => [:help, :forgot_password]
  # use :force_mobile_format instead to test
  # before_filter :force_mobile_format

Then add the line below to config/initializers/mime_types.rb

Mime::Type.register_alias "text/html", :mobile

I recommend that you setup a before_filter that will redirect to a specific page depending on whether or not it is a mobile request. How can you check this?

is_mobile_device? # => Returns true or false depending on the device

You can also determine which format is currently set in by calling the following:

in_mobile_view? # => Returns true or false depending on current req. format

Also, if you want the ability to allow a user to switch between 'mobile' and 'standard' format (:html), you can just adjust the mobile_view session variable in a custom controller action.

session[:mobile_view] # => Set to true if request format is :mobile and false
                           if set to :html

*FORK NOTE:* I found this to be a fairly unreliable means of doing this, and that's why I introduced the bypass mechanism.


If you have some custom controller action that allows the user to toggle the mobile formatting, you can utilize these methods for great success:

bypass_mobile_view        # => Causes mobile formatting to be switched off for
                                the session
bypass_mobile_view(false) # => Turns off the bypass

You can avoid calling these methods yourself, set_mobile_format will check to see if a 'bypass' parameter is present in the request. If present and it equals the string 'true', bypass will be enabled (if set to anything else, it will be disabled). Thus, you can create links like this for the user:

= link_to 'Exit mobile view', some_path(:bypass => :true)
= link_to 'Switch to mobile view', some_path(:bypass => :false)

*To do:* Allow user to define custom parameter for toggling the bypass.

Custom Style Over-rides Based on Device

So, different devices need different styling. Don't worry, we've got this baked in to Mobile Fu.

If you are including a css or sass file via stylesheet_link_tag, all you have to do is add _device to the name of one of your files to override your styling for a certain device. The stylesheet that is loaded is dependant on which device is making the request.

e.g., Accessing a page from a Blackberry.

...  stylesheet_link_tag 'mobile.css' ...

This loads mobile.css, and mobile_blackberry.css if the file exists.

Supported stylesheet override device extensions at the moment are:

iphone (iphone,ipod)

The stylesheet awesomeness was derived from Michael Bleigh's browserized styles:

Inspiration for Mobile Fu came from Noel Rappin's rails_iui:

Hopefully this should help you create some awesome mobile applications.

Testing Mobile Interface

If you want to force the mobile interface for testing, you can either use a mobile device emulator, or you can pass 'true' to has_mobile_fu.

class ApplicationController < ActionController::Base

Copyright © 2008 Brendan G. Lim, Intridea, Inc., released under the MIT license

Something went wrong with that request. Please try again.