How to change layouts based on every action in Rails.
We have more than one layouts for individual action and need to change this based on action or page, how to do that.
Very easy, first you can create layouts in layouts folder as like
- about_layout.html.erb
- home_layout.html.erb
- contact_layout.html.erb
- product_layout.html.erb
now in the controller
class PagesController < ApplicationController layout :determine_layout def home end def product end def about end def contact end private def determine_layout case action_name when "home" "home_layout" when "product" "product_layout" when "about" #=> or you can use a comma for the same layout in 2 or 3 actions like when "home","contact" "about_layout" when "contact" "contact_layout" end end end
This called action base layout.