Layouts in Rails

(Brock Weaver) #1

I apologize if this the incorrect mailing list, as it is a Rails question,
not really a Ruby question. Anyway:

To apply a layout to a controller, you can say:

class UsersController < ApplicationController
layout "master"
end

...which you would need to do in every controller. So my first thought is to
push this down into ApplicationController, mitigating the need to put
anything layout related in all the other controllers:

class ApplicationController < ActionController::Base
layout "master"
end

...which doesn't work. So my next thought is to declare an instance variable
in the ApplicationController:

class ApplicationController < ActionController::Base
@layout = "master"
end

and change all other controllers to look like this:

class UsersController < ApplicationController
layout @layout
end

...which doesn't work either. So I grudgingly try a global with the same
thought process:

class ApplicationController < ActionController::Base
$layout = "master"
end

class UsersController < ApplicationController
layout $layout
end

...which works. But I don't understand why, if UsersController derives from
ApplicationController, the other two methodologies fail. Can someone explain
this to me?

Centralizing the layout moniker to one place will help significantly as I
move my app from test/dev/prod, so I can have alarmingly different layouts
for test/dev versus production.

···

--
Brock Weaver
http://www.circaware.com

(Sean T Allen) #2

putting layout in ApplicationController always works for me...

perhaps you have an odd typo or something somewhere?

(Brock Weaver) #3

Thanks for the lead, Sean. I'd used the generate scaffold to create the
usuals, and it created users.rhtml. When this file exists, it seems to
ignore the layout method in my ApplicationController, as I'm guessing the
users.rhtml is more specific / overrides that value.

Deleting the users.rhtml file resulted in the expected behavior. Thanks
again!

···

On 8/12/05, Sean T Allen <sean@ardismg.com> wrote:

putting layout in ApplicationController always works for me...

perhaps you have an odd typo or something somewhere?

--
Brock Weaver
http://www.circaware.com