Detecting mobile browsers in Rails 3

后端 未结 3 582
逝去的感伤
逝去的感伤 2021-01-03 23:04

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.

I came across

3条回答
  •  醉话见心
    2021-01-03 23:18

    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.

    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
    

提交回复
热议问题