Forum: Ruby on Rails Admin::Contr does not use layouts/admin/application.rhtml!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-03 14:41
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
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2007-04-03 14:56
(Received via mailing list)
Joshua Muheim 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.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-03 18: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?
D1f1c20467562fc1d8c8aa0d328def62?d=identicon&s=25 Florian Gilcher (skade)
on 2007-04-03 19: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.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-03 19:45
Florian Gilcher 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.
Af31e731dc3f44620a7874d9602e5bc8?d=identicon&s=25 Evan (Guest)
on 2007-04-04 08: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.
Af31e731dc3f44620a7874d9602e5bc8?d=identicon&s=25 Evan (Guest)
on 2007-04-04 08: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
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-07 10: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
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-07 11: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?
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-07 11: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? ;-)
This topic is locked and can not be replied to.