Skip to content

Instantly share code, notes, and snippets.

@cscotta
Created November 19, 2008 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cscotta/26800 to your computer and use it in GitHub Desktop.
Save cscotta/26800 to your computer and use it in GitHub Desktop.

Redirecting smart and dumb mobile browsers in Interface ERB templates
-—————————————————————————————————

In the page you’d like to redirect (or _header.html.erb) -

<%= mobile_redirect(‘http://www.google.com’, ‘http://www.microsoft.com’) %>

The first argument is the address for smart mobile devices defined in the smart_list array (see mobile_redirect method).

The second argument is the address for dumb mobile devices.

If you’d prefer not to redirect a smart or dumb mobile device, pass an empty string:

<%= mobile_redirect(‘http://www.google.com’, ’’) %>

Include this in client_helper.rb:

def mobile_redirect(smart_url, mobile_url)
agent = request.user_agent.downcase

smart_list = [‘iphone’, ‘ipod’, ‘android’, ‘opera mini’] mobile_list = [‘mobi’, ‘pocket’, ‘palm’, ‘nokia’, ‘phone’, ‘symbian’, ‘windows ce’ ‘blackberry’, ‘opwv’, ‘avant’, ‘docomo’, ‘wii’, ‘samsung’, ‘playstation’] smart = smart_list.detect { |mobile| agent.match(mobile) } mobile = mobile_list.detect { |mobile| agent.match(mobile) } if smart && smart_url != ’’ return ‘’ + smart_url + ‘" />’ elsif mobile && mobile_url != ’’ return ‘’ + mobile_url + ‘" />’ else return "" end end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment