问题描述
我的设置:Rails 3.0.9,Ruby 1.9.2
My setup: Rails 3.0.9, Ruby 1.9.2
我的应用需要根据请求的来源来提供移动版和网页版.我需要支持所有主要的移动客户端前端,例如iPhone,Android,Blackberry等.在我的代码中检测到此错误的最简单方法是什么?
My app needs to serve up a mobile vs. web layout depending on the request's origin. I need to support all the major mobile client front-ends like iPhone, Android, Blackberry, etc. What's the simplest way to detect this in my code?
推荐答案
最简单的方法是通过RegEx /Mobile | webOS/解析 request.user_agent .可以将Mobile/Full版本变量保存到会话中,帮助程序将有助于包含移动CSS:
The easiest way to do it is parse request.user_agent by RegEx /Mobile|webOS/.Mobile/Full version variable can be saved into session, and helper will be useful to include mobile CSS:
#controller
def mobile_device?
if session[:mobile_param]
session[:mobile_param] == "1"
else
request.user_agent =~ /Mobile|webOS/
end
end
helper_method :mobile_device?
#layout
<%= stylesheet_link_tag 'mobile' if mobile_device? %>
Railscasts 199 是为您提供的分步指南.
Railscasts 199 is a step-by-step guide for you.
这篇关于Rails 3检测来自移动客户端的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!