Is it possible to reset a default layout from within a before_filter method in Rails 3?
I have the following as my contacts_controller.rb:
cla
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"
end
end
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?
end
end
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" }