5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails4.1のvariantsでスマホ用にテンプレートを切り替える

Posted at

Rails4.1にvariantsという条件によってテンプレートを変える便利機能があります。

使い方

  • コントローラでrequest.variantsをセット
  • バリアントごとにテンプレートを用意

これだけです。

##ApplicationControllerでの設定例

class ApplicationController < ActionController::Base
  before_action :detect_variant

  private
  def detect_variant
    case request.user_agent
    when /ip(hone|od)/i
      request.variant = :phone
    when /android.+mobile/i
      request.variant = :phone
    end
  end
end

テンプレートの準備

index.html.hamlindex.html+phone.hamlのように
アクション名.拡張子+request.variant.テンプレートエンジン というテンプレートを別に用意する

参考

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?