问题描述
我希望在我的应用中做一些特定于移动设备的布局,并且一直在研究检测移动浏览器和提供特定于移动设备布局的方法.
I'm looking to do some mobile-specific layouts in my app, and have been researching ways to detect mobile browsers and serve mobile specific layouts.
我遇到了这个:http://www.arctickiwi.com/blog/mobile-enable-your-ruby-on-rails-site-for-small-screens
但是使用一系列关键字对我来说似乎有点脆弱.如果关键字发生变化怎么办?任何人都遇到过任何 gem 或插件或任何更多面向未来的策略?
But using an array of keywords seems a little fragile to me. What if the keywords change? Anyone come across any gems or plugins or any more future proof strategies?
推荐答案
实际上您可以使用更简单的正则表达式.此Railscast 中概述了该方法,并允许您加载不同的 JS 并定义不同的页面交互适用于移动设备.
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.
本质上,您最终会得到一个函数,它只是检查用户代理是否包含Mobile"或webOS":
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
这篇关于在 Rails 3 中检测移动浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!