LoginSignup
14
8

More than 5 years have passed since last update.

Railsのデバイス判定にbrowser

Posted at

ブラウザ判定(IEやFirefox、Chromeとか)やデバイスの判定(PCかスマホか)したい時には大体UserAgentつかうでしょうが、最近は複雑化しているから対応が簡単でないですね。
RailsはこれをやるためにbrowserというGemがあります。超便利。
https://github.com/fnando/browser

しかし、ここのReadme通りにインストールすると依存関係の問題か0.9.1(2016年8月現在)しか入らないです。
ということで自分なりの使い方ですが、Gemfileに

Gemfile
# browser detection
gem 'browser', '~> 2.2.0'

と書きます。

あとはいつものbundle install

Viewの箇所には
if browser.device.mobile?

とヘルパーbrowserを入れるのでOKです。他にもbrowser.ie?(6) とかbrowser.platform.android?(4.2)とかbrowser.device.xbox?なんて出来る。

便利ですな。

14
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
8