42

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 Detect to 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.

Is there a way to do something similar with Ruby 2.0.0 and Rails 4.0.0 ?

Maybe there is a gem to handle cases like that?

Severin
  • 8,508
  • 14
  • 68
  • 117

1 Answers1

94

Check the request method, where you can get a ActionDispatch::Request where you have all the request parameters, including the user agent.

request.user_agent
Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • 6
    Thanks. I don't see this helpful tip mentioned on either of your two links any more. – MSC Feb 05 '19 at 15:04
  • The `user_agent` helper is defined in the [`Rack::Request::Helpers` module](https://www.rubydoc.info/gems/rack/Rack/Request/Helpers#user_agent-instance_method) (which is included in `Action::Dispatch::Request`) – Holger Just Jun 07 '23 at 16:32