積々RUNMIZZO

日々積み重ね

variantsによるテンプレートの切り替え

メンチキッター作っててPCでの見え方とスマホからの見え方は変えて行こうと思ってるのでvariantsの機能は覚えとこう、今の追加機能実装してUI整理するときに使いたい。。

  • 接続端末によってPCとは別のテンプレートを表示できる

  • index.html.erbだったものがindex.html+mobile.erbのようにしてテンプレートが選択

ざっくりメモ✍️

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
    before_action :detect_mobile_variant

    private

    def detect_mobile_variant
        request.variant = :mobile if request.user_agent =~ /iPhone/
    end
end
<!-- app/views/books/show.html+mobile.erb -->

<h1>本の情報(for mobile)</h1>