Hi all I know that Rails uses the layout views/layouts/application.rhtml as default when not specified differently. But why do controllers in modules not also use their own application.rhtml? controllers/MyController => uses views/layouts/application.rhtml controllers/admin/MyController => also uses views/layouts/application.rhtml, but I'd like it to use uses views/admin/layouts/application.rhtml! Is this a bug or just a neglection in the specification of Rails? Greetz Josh
on 2007-04-03 16:41
on 2007-04-03 16:56
Joshua M. wrote: > views/admin/layouts/application.rhtml! > > Is this a bug or just a neglection in the specification of Rails? > > Greetz > Josh > > Reading this might help you: http://www.microisv.com.ph/blog/layout-tips-using-... Cheers Mohit.
on 2007-04-03 20:07
Thanks, but that's all familiar to me. My question is a different one: Why does MyModule/MyController use layouts/application.rhtml as layout and not layouts/my_module/application.rhtml?
on 2007-04-03 21:17
If the layout is not set explicitly, and the default layout for the controller is not present, the parents layout is chosen. Default layouts are chosen by controller name. As the parent of MyModule::Testcontroller is ::ApplicationController (without module), the path to the parents layout is /application.rhtml and not my_module/application.rhml (because ApplicationController itself has no module). This is not a bug.
on 2007-04-03 21:45
Florian G. wrote: > If the layout is not set explicitly, and the default layout for the > controller is not present, the parents layout is chosen. Default layouts > are chosen by controller name. > > As the parent of MyModule::Testcontroller is ::ApplicationController > (without module), the path to the parents layout is /application.rhtml > and not my_module/application.rhml (because ApplicationController itself > has no module). > > This is not a bug. Thanks a lot, this sounds very reasonable. I created now a file controllers/admin/application.rb with the following content: class Admin::ApplicationController < ApplicationController end Although I require it in controllers/application.rb it seems not to be existend because when trying to extend my Admin::CountryController from Admin::ApplicationController I get the following error: uninitialized constant Admin::ApplicationController Anybody can help me? Thanks.
on 2007-04-04 10:33
You should probably renamed your controller file to app/controllers/admin/application_controller.rb By default a user-defined controller named MyController would look for a file my_controller.rb Note the appended _controller, as only the default application controller looks for app/controllers/application.rb. Since your Admin::ApplicationController is user-defined, it has to look for app/controllers/admin/application_controller.rb instead. I hope this helps.
on 2007-04-04 10:41
Please scrap my last reply. I think you want to use your admin/application.rb layout in all admin controllers, so here's what you should put in your Admin::ApplicationController class instead: class Admin::ApplicationController < ApplicationController layout 'admin/application.rb' end If you call: class Admin::MyController < Admin::ApplicationController end it will use the admin/application.rb layout
on 2007-04-07 12:42
> class Admin::ApplicationController < ApplicationController > layout 'admin/application.rb' > end Thank you! I didn't even need the layout() call, it used the admin/layouts/application.rhtml automatically. :-) Now my only question is: how can I prevent somebody from calling /admin/application (which loads admin/application_controller.rb)? I want it to result in a routing error like when calling /application (which does *not* load application.rb)... Thanks a lot for help
on 2007-04-07 13:23
Well, I just renamed the file admin/application_controller.rb to admin/application.rb and now I get the desired routing error when calling admin/application But sadly I have to add the line require File.dirname(__FILE__) + '/application' to every controller in the Admin module. I'd like Rails to search for a file application.rb in every Module and to load it if available (like it does with the default application.rb file). Is there a way to accomplish this?
on 2007-04-07 13:29
Well, I just placed the definition of Admin::ApplicationController into the controllers/application.rb file. Problem solved. :-P Why do it easily if there's a more complicated way? ;-)