Rails方式来检测移动设备? [英] Rails way to detect mobile device?

查看:76
本文介绍了Rails方式来检测移动设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法"来检测用户是否正在使用移动设备?我的意思是可以在erb中使用的方法,如下所示:

Is there a "rails way" to detect whether the user is using a mobile device? What I mean is a method I can use in erb, something like this:

<% if mobile? %>

推荐答案

您可以通过定义如下函数来做到这一点:

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

或者您可以使用gem来检测

Or you can use gems to detect mobile devices like

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

这篇关于Rails方式来检测移动设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆