💎Rails tip: Template variants

Sebastien Auriault
Mar 12, 2022
Did you know about template variants? Useful when you have let's say one view for admins and one for normal users. Or one for mobile and one for desktop (when the views are too different to just be responsive).

class DashboardsController < ApplicationController
  def show
    request.variant = Current.user.admin? ? :admin : :user
  end
end

# If admin:
# app/views/dashboards/show.html+admin.erb

# If normal user:
# app/views/dashboards/show.html+user.erb