Rails set layout from within a before_filter method
From Rails guides, 2.2.13.2 Choosing Layouts at Runtime
:
class ProductsController < ApplicationController layout :products_layout private def products_layout @current_user.special? ? "special" : "products" endend
If for some reason you cannot modify the existing controller and/or just want to do this in a before filter you can use self.class.layout :special
here is an example:
class ProductsController < ApplicationController layout :products before_filter :set_special_layout private def set_special_layout self.class.layout :special if @current_user.special? endend
It's just another way to do essential the same thing. More options make for happier programmers!!
The modern way of doing this is to use a proc,
layout proc { |controller| user.logged_in? "layout1" : "layout2" }