Rendering different layouts in Rails depending on whether the user is logged in

Assuming we have two different layout files:

We can add the following bit of code in our base ApplicationController:

class ApplicationController < ActionController::Base
  layout :layout


  def layout
    user_signed_in? ? 'application' : 'guest'

Assuming all our custom controllers extend ApplicationController as is the default convention, then our views will use the application layout when the user is signed in and the guest layout when they aren’t signed in.

Sajad Torkamani

Hello! My name is Sajad. I’m a software developer at Nationwide and an aspiring entrepreneur. Originally from Afghanistan, I moved to London at an early age and have been living here for the past 20 years.

This is my personal website where I blog mostly about programming with occasional thoughts on a bunch of other things. I write mainly for my own benefit as I find that writing helps clarify my otherwise very confused thoughts.

Get in touch: