[英]Rails 3 detect request coming from mobile clients
我的設置:Rails 3.0.9、Ruby 1.9.2
我的應用程序需要根據請求的來源提供移動與 web 布局。 我需要支持所有主要的移動客戶端前端,如 iPhone、Android、Blackberry 等。在我的代碼中檢測到這一點的最簡單方法是什么?
最簡單的方法是通過 RegEx /Mobile|webOS/解析request.user_agent 。 移動版/完整版變量可以保存到 session 中,幫助器將有助於包含移動版 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是為您提供的分步指南。
它將返回用戶代理。
request.user_agent =~ /Mobile|Blackberry|Android/ # OR WHATEVER
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.