Hi,
I am trying to select a layout for a page based on what action is called.
ex.
if controller.action_name == ‘about’
layout “internal”
else
layout “home_static”
end
But I am receiving this error:
undefined local variable or method `controller' for HomeController:Class
Is there any way that I can do this? Is this even possible?
Thanks,
Jonathon
Hmm, not sure if this will work, but since the controller is the
object and the "actions" are just methods then:
if self.respond_to?(about)
layout "internal"
else
layout "home_static"
end
I'm not sure whether layout can be called conditionally this way, but
I *am* sure that "controller" is an instance variable, not a local
variable. Try something like:
if @controller.action_name == 'about'
layout "internal"
else
layout "home_static"
end
Craig
Gene_Tani
(Gene Tani)
July 14, 2007, 2:49am
4
Found out the answer.
layout :choose_layout
private
def choose_layout
if [ 'signup', 'login' ].include? action_name
'login'
else
'admin'
end
end
This works perfectly.
From here: http://paulsturgess.co.uk/articles/show/41-how-to-specify-multiple-layouts-within-the-same-controller-in-ruby-on-rails