Ruby-on-rails 在 Rails 4 中检测用户代理 - 读取 HTTP 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19535397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect user agent in Rails 4 - read HTTP header
提问by Severin
I just switched from PHP to Ruby on Rails and was wondering if there was a way to detect the clients device/user agent (reading the HTTP header) in order to serve different versions of the site depending on the request it gets. In PHP I have been using Mobile Detectto do so. The general idea is to only serve files that are needed for each particular version. Thats why a client side approach is not that effective.
我刚刚从 PHP 切换到 Ruby on Rails,想知道是否有一种方法可以检测客户端设备/用户代理(读取 HTTP 标头),以便根据它获得的请求为不同版本的站点提供服务。在 PHP 中,我一直在使用Mobile Detect来做到这一点。总体思路是仅提供每个特定版本所需的文件。这就是为什么客户端方法不是那么有效。
Is there a way to do something similar with Ruby 2.0.0 and Rails 4.0.0 ?
有没有办法用 Ruby 2.0.0 和 Rails 4.0.0 做类似的事情?
Maybe there is a gem to handle cases like that?
也许有一个宝石可以处理这样的情况?
回答by Paulo Fidalgo
Check the requestmethod, where you can get a ActionDispatch::Requestwhere you have all the request parameters, including the user agent.
检查请求方法,您可以在其中获取ActionDispatch::Request,其中包含所有请求参数,包括用户代理。
request.user_agent

