Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a "rails" way to redirect if mobile browser is detected?

I want to run a user-agent check but only on my homepage (static_controller#home) I have taken a close look at the code: http://www.arctickiwi.com/blog/mobile-enable-your-ruby-on-rails-site-for-small-screens and it seems close but it never gets tied together, its just method creation... I am open to a jquery method but would prefer ruby/rails over js.

I only really care about iphone and droid...

like image 224
TJ Sherrill Avatar asked Aug 01 '11 23:08

TJ Sherrill


3 Answers

Check out this screencast. They suggest using the following for detecting a mobile device:

request.user_agent =~ /Mobile|webOS/
like image 57
Mischa Avatar answered Nov 20 '22 10:11

Mischa


http://detectmobilebrowsers.com/

Extremely useful for me.

like image 21
Kirk Avatar answered Nov 20 '22 12:11

Kirk


From this SO answer:

The best way is to use some supported plugin/gem, like browser

This is much better than writing your own regex.

For example Opera 11.50 has the following user_agent:

Opera/9.80 (Android 2.3.7; Linux; Opera Mobi/ADR-1111021303; U; en-GB) Presto/2.9.201 Version/11.50

The suggested regex of:

request.user_agent =~ /Mobile|webOS/

would total miss that.

You also get the benefit of knowing what platform (mac, windows etc), tablets, ie6 etc. I am a rookie rails dev, I set it up and got it working in like 2 minutes. Highly recommended.

like image 21
Joshua Dance Avatar answered Nov 20 '22 10:11

Joshua Dance