Routing and namespace

I’m trying to set up and ‘admin’ interface for each of my models.

I have a controller for each of them under ‘app/controllers/admin/’
book_controller.rb
page_controller.rb
and so on.

So as not to get in the way of normal use I’m ‘forcing’ a prefix in the
routing/url with lines in routes.rb like

map.connect ‘/admin/page/:page/:action’,
:controller => ‘admin/page_controller’,
:action => ‘show’,
:requirements => { :page => page_name_requirement }

when I experiment with app.url_for() in the console I get the results I
expect.

But when I fire up the server and try to go to

http://localhost.:3000/admin/page/ChapterOne

I get

NameError in Admin/web controllerController#list

uninitialized constant Admin::WebControllerController

Two questions:

Should my controller read:

class Admin::WebController < ApplicationController

which was how it was generated

and/or should the routing be fudged in some way.

Remember, this is not the ‘real’ application.
The real application comes out at

   http://localhost.:3000/TheStoryofLitchfileld/ChapterOne

and the routing for that is lower down in the routes.rb

“Quality refers to the extent to which processes, products, services,
and
relationships are free from defects, constraints and items which do not
add
value.”

  • Dr. Mildred G Pryor, 1995

If you have a controller in app/controllers/admin/book_controller.rb
defined like this:

class Admin::BookController < ApplicationController

Then the default route to the controller will be /admin/book. There
is no need to add anything to routes.rb, it will “just work”.

Paul B. said the following on 02/19/2007 12:16 PM:

If you have a controller in app/controllers/admin/book_controller.rb
defined like this:

class Admin::BookController < ApplicationController

Then the default route to the controller will be /admin/book. There
is no need to add anything to routes.rb, it will “just work”.

Ah.
I see, that picks up on the default.

Now what about my custom application route, then.
It is supposed to be

   http://localhost:3000/TheStoryofLitchfileld/ChapterOne

aka “:book/:chapter” and that goes to the book_chapter_controller.rb

There is path before it to indicate the namespace or the controller.


Always do your best. What you plant now, you will harvest later.

  • Og Mandino