I had a hard time finding any documentation on if or how layout files worked with nested controllers. After trying a few thing myself (e.g. adding a admin/users.rhtml file and a admin_users.rhtml file to the layouts directory) and not seeing the expected results, I decided to look at the source to see if support for this was included. After looking around in the Layouts Module I learned that one needs to first create a "controller" directory inside of layouts and then the natural admin/users.rhtml file would work. I''m wondering if it might be easier to eliminate this extraneous directory? Is there a reason for this current behavior that I am just unfamiliar with (like Components or something)? Thanks. -- DeLynn Berry delynn@gmail.com http://www.delynnberry.com
Another quick observation. Would it also make sense for a Admin::UsersController to first look for a layouts/admin/application.rhtml file first before looking for the layouts/admin/users.rhtml file? This would allow one to easily create different layouts for the different sub-sections of an application without having to specify the layout in the controller or creating a seperate layout file for every controller in their "admin" section. -- DeLynn Berry delynn@gmail.com http://www.delynnberry.com On 2/3/06, DeLynn Berry <delynn@gmail.com> wrote:> I had a hard time finding any documentation on if or how layout files > worked with nested controllers. After trying a few thing myself (e.g. > adding a admin/users.rhtml file and a admin_users.rhtml file to the > layouts directory) and not seeing the expected results, I decided to > look at the source to see if support for this was included. After > looking around in the Layouts Module I learned that one needs to first > create a "controller" directory inside of layouts and then the natural > admin/users.rhtml file would work. > > I''m wondering if it might be easier to eliminate this extraneous > directory? Is there a reason for this current behavior that I am just > unfamiliar with (like Components or something)? > > Thanks. > > -- > DeLynn Berry > delynn@gmail.com > http://www.delynnberry.com
On Feb 3, 2006, at 4:19 PM, DeLynn Berry wrote:> Another quick observation. > > Would it also make sense for a Admin::UsersController to first look > for a layouts/admin/application.rhtml file first before looking for > the layouts/admin/users.rhtml file? > > This would allow one to easily create different layouts for the > different sub-sections of an application without having to specify the > layout in the controller or creating a seperate layout file for every > controller in their "admin" section. >I think that may cause a little confusion if there''s a special check for the ''application'' layout within subdirectories. I suggest making a BaseController from which other controllers inherit in that directory... e.g. class Admin::BaseController < ApplicationController layout ''admin'' end class Admin::UsersController < Admin::BaseController # etc. end This has worked well for me in the past where I''ve needed some simple but shared functionality within a "module" or group of controllers. I like your previous suggestion of removing the extraneous "controller" directory from the ''layouts'' directory. I didn''t even know that was the current requirement. Just thinking out loud... the same principle would make the auto- inclusion of javascripts and stylesheets more sensible also-- something that I''ve been trying to find a solution for within my bundled_resource plugin. For example: public/javascripts/admin/ new.js, or public/stylesheets/admin/base.css. Of course, this only works with assets and resources that assume quite a bit about the app''s controllers and views. Duane Johnson (canadaduane) http://blog.inquirylabs.com/
> I think that may cause a little confusion if there''s a special check > for the ''application'' layout within subdirectories. I suggest > making a BaseController from which other controllers inherit in that > directory...I agree with you, and your suggestion was how I had solved this problem previously. I was just trying to eliminate the need for the layout method for these nested controllers as well (DRYing layout sharing for nested controllers even more ;-).> I like your previous suggestion of removing the extraneous > "controller" directory from the ''layouts'' directory. I didn''t even > know that was the current requirement.I just submitted a patch that fixes this issue and also enhances the documentation to explain how to get the auto-discovery of layout files to work with nested controllers. This fix, along with the creation of a BaseController for each of the nested sections of an application, creates what I think is a very nice solution to the problem. If you have an Admin::BaseController < ApplicationController, the layout that is first looked for is app/views/layouts/admin/base.rhtml. I really like this solution to layout sharing for nested controllers. Hopefully it will get applied before 1.1 ships. -- DeLynn Berry delynn@gmail.com http://www.delynnberry.com
> I just submitted a patch that fixes this issue and also enhances the > documentation to explain how to get the auto-discovery of layout files > to work with nested controllers.I guess it might have been good to include the url to the patch :-) http://dev.rubyonrails.org/ticket/3747. -- DeLynn Berry delynn@gmail.com http://www.delynnberry.com