Forum: Ruby on Rails setting the right routes

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.
Saad S. (Guest)
on 2006-02-17 01:43
I'm trying to create an admin section in my application which can be
accessed through www.myapp.com/admin.  I'm going to need to administer
two models through the admin section.  I have a generated the
controllers for Admin::Image and Admin::Project.  That created a
subdirectory under the controllers directory called admin and I can
access each controller through www.myapp.com/admin/image or
www.myapp.com/admin/project.

Now what I want to do is make a root admin page from which
administrators will be able to select whether they want to go to the
image admin or the project admin.  I tried creating an admin controller
in the top level directory but then when I go to
www.myapp.com/admin/image it says no action 'image' found in controller
'admin'.  So it's assuming that i'm looking in the controller admin for
the action 'image' when really what i want is for it to go to the image
admin section.

what's the best way to set this up?
Joshua S. (Guest)
on 2006-02-17 03:48
You can't have a controller with the same name as a module. (At least
when I tried that I ran into problems that I didn't know how to work
around.)

Is there a big reason you split the image and project admin functions
into separate controllers? Perhaps you are being lazy and relying on
scaffolding? If so, it's probably simplest to just have one admin
controller that can work on both images and projects. I recently did the
same thing myself and it works just fine.  Also makes handling the
authentication for admin functions simple, as everything is in one
controller.

If you want to keep the controllers separate, try this.  Create a third
controller, Admin::Root. Your routes would be something like:

map.connect 'admin' :controller => 'admin/root', :action => 'index'
map.connect ':controller/:action/:id'

In the default route, :controller will map to the controller in the
module, either admin/root, admin/image or admin/project.

--josh

Saad Shakhshir wrote:
> I'm trying to create an admin section in my application which can be
> accessed through www.myapp.com/admin.  I'm going to need to administer
> two models through the admin section.  I have a generated the
> controllers for Admin::Image and Admin::Project.  That created a
> subdirectory under the controllers directory called admin and I can
> access each controller through www.myapp.com/admin/image or
> www.myapp.com/admin/project.
>
> Now what I want to do is make a root admin page from which
> administrators will be able to select whether they want to go to the
> image admin or the project admin.  I tried creating an admin controller
> in the top level directory but then when I go to
> www.myapp.com/admin/image it says no action 'image' found in controller
> 'admin'.  So it's assuming that i'm looking in the controller admin for
> the action 'image' when really what i want is for it to go to the image
> admin section.
>
> what's the best way to set this up?
This topic is locked and can not be replied to.