I'm trying to leverage Rails Variants to use a different layout for phones, and a different one (default one) for tablets+desktops.
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :detect_device_format
private
def detect_device_format
case request.user_agent
when /iPad/i
request.variant = :tablet
when /iPhone/i
request.variant = :phone
when /Android/i && /mobile/i
request.variant = :phone
when /Android/i
request.variant = :tablet
when /Windows Phone/i
request.variant = :phone
end
end
end
class HomeController < ApplicationController
def index
respond_to do |format|
format.json
format.html # /app/views/home/index.html.erb
format.html.phone # /app/views/home/index.html+phone.erb
format.html.tablet # /app/views/home/index.html+tablet.erb
end
end
end
Now, I know I can use something like format.html.phone { render layout: 'application-mobile' }
but I don't want to do this every single time.
I'd like to keep things dry and create a default phone layout.
How can I accomplish this using Rails 4.1?