Admin::Contr does not use layouts/admin/application.rhtml!

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

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:

Cheers
Mohit.

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?

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.

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.

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.

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

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?

Well, I just placed the definition of Admin::ApplicationController into
the controllers/application.rb file. Problem solved. :stuck_out_tongue:

Why do it easily if there’s a more complicated way? :wink:

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. :slight_smile:

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