PCとSmartPhoneの表示するViewを切り替えるための設定をします。
便利なgemがあるのでそちらを活用します。
まずはgemのinstall
Gemfile
gem 'rack-user_agent'
これでbundle install。
以下のような感じにapplicationコントローラー内で設定します。
/app/controller/application_controller.rb
class ApplicationController < ActionController::Base
before_action :set_request_variant
private
def set_request_variant
if from_smartphone?
request.variant = :smartphone
end
end
def from_smartphone?
request.from_android? || request.from_iphone? || request.from_ipod? || request.from_windows_phone?
end
end
あとは、index.html.haml
と index.html+smartphone.haml
を作成すればデバイスによって使用するViewのファイルを切り分けてくれます。
PCで開くと通常のViewファイルが表示され、smartphoneで開くとsmartphone
がついたファイルが表示されます。