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?
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).
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:
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
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?