views:

38

answers:

1

It's in my understanding that I can specify a layout for a controller by putting

layout 'mylayout'

in the controller file. I further understand that I can specify certain actions that the layout will be invoked for with :only and :except as such: layout 'mylayout', :only => [:index, :new] or layout 'mylayout', :except => [:index, :new]

What I am wondering is, can I put layout 'mylayout' in my application controller and specify that to be rendered for only certain controllers with something like :only_controller and :except_controller, or do I have to specify layout 'mylayout' in every controller I want it to be rendered in?

The reason for this is that I have a few controllers that are in charge of the admin section of my site, and I'd like to have a different layout for them.

+1  A: 

Have the admin controllers inherit from an AdminController and create an admin.html.erb layout. That way all the controllers for your admin section will have this layout.

neutrino
But I don't need an actual physical admin controller - it does not exist. If I create it - will it have to have a database backend?
yuval
There's no way controllers are associated with database :) think of it just as an abstract base class for your admin controllers. It will even make things clearer, because when you see a controller inheriting from `AdminController` you'll know at once that it's a controller from admin section. Also, it may well be that you'll need some common `before_filter` s, and a base class would make a perfect place for them.
neutrino
thank you very much!
yuval