Rails Way to Detect Mobile Device

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

Or you can use gems to detect mobile devices like

  • https://github.com/tscolari/mobylette
  • https://github.com/shenoudab/active_device

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

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

request.user_agent =~ /Mobile|webOS/

Detect web-browser or mobile to display links

Browser data comes in the form of the user_agent object:

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
helper_method :mobile?

private

def mobile? # has to be in here because it has access to "request"
request.user_agent =~ /\b(Android|iPhone|iPad|Windows Phone|Opera Mobi|Kindle|BackBerry|PlayBook)\b/i
end
end

This will allow you to use the following in your views:

#app/views/controller/view.html.erb
<% if mobile? %>
... do something for mobile
<% else %>
... do something else
<% end %>

Being honest, I don't get why people hard-code their mobile interface work. If you use CSS media queries properly, you shouldn't have an issue with the files, although your case certainly appears to require the server-side conditions.

Detecting mobile browsers in Rails 3

There is actually a much simpler regular expression you can use. The approach is outlined in this Railscast and allows you to load different JS and define different page interactions for mobile devices.

Essentially you end up with a function that simply checks that the user-agent contains 'Mobile' or 'webOS':

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

How to detect mobile phone from http request using rack middleware

There is a gem that may be helpful for that https://github.com/talison/rack-mobile-detect
Also I suggest considering this gem github.com/jistr/mobvious as it detects type of device(mobile, tablet, desktop)

What's the easiest way to detect the user's operating system in Rails 3?

if request.env['HTTP_USER_AGENT'].downcase.match(/android|iphone/)
puts "yup, its mobile"
end

Rails 3 Group Mobile & Tablets w/ User Agent?

I use this one

def mobile_agent?
request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(iPhone|iPad|iPod|BlackBerry|Android)/]
end


Related Topics



Leave a reply



Submit