jwm
July 13, 2007, 10:27pm
1
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
jwm
July 13, 2007, 11:05pm
2
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
On Jul 13, 4:27 pm, “Jonathon Paul M.”
jwm
July 14, 2007, 3:19am
3
On 7/13/07, Jonathon Paul M. [email protected] wrote:
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
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
jwm
July 16, 2007, 8:40pm
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
jwm
July 14, 2007, 4:49am
5
On Jul 13, 1:27 pm, “Jonathon Paul M.”
[email protected] wrote:
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, interesting question, you could look at how Radiant or Typo or
other apps do themes, e.g.
http://mattmccray.com/svn/rails/plugins/theme_support/
http://ragnarson.blogspot.com/2006/04/dynamic-templateroot-revisited.html
http://ragnarson.blogspot.com/2006/03/dynamic-templateroot.html
http://ostracons.com/entry/259/full-plates