Scoped Controller to URL Mapping (namespace?)

$ ruby script/generate controller NewAccount::Business
$ ruby script/generate controller NewAccount::Personal
$ ruby script/generate controller NewAccount

This doesn't work because the NewAccountController class gets all the
incoming requests (URLs prefixed with "new_account"). What I would like
is a conventional way for the following URLs to work without using
routes.rb.

Call index action in NewAccountController:
http://host.net/new_account

Call index action in NewAccount::BusinessController:
http://host.net/new_account/business

Call index action in NewAccount::PersonalController:
http://host.net/new_account/personal

What I would like to have happen is have the NewAccountController
render a page with links to the scoped controllers, ie.
NewAccount::[Business|Personal]Controller. Basically, the URL at
/new_account will give an overview of the Personal and Business
accounts and provide links to each controller's index action.

It seems like there may be a better way. Any help is much appreciated.

-pachl

Reply

clintpachl wrote:

$ ruby script/generate controller NewAccount::Business
$ ruby script/generate controller NewAccount::Personal
$ ruby script/generate controller NewAccount

This doesn't work because the NewAccountController class gets all the
incoming requests (URLs prefixed with "new_account"). What I would like
is a conventional way for the following URLs to work without using
routes.rb.

Call index action in NewAccountController:
http://host.net/new_account

Call index action in NewAccount::BusinessController:
http://host.net/new_account/business

Call index action in NewAccount::PersonalController:
http://host.net/new_account/personal

What I would like to have happen is have the NewAccountController
render a page with links to the scoped controllers, ie.
NewAccount::[Business|Personal]Controller. Basically, the URL at
/new_account will give an overview of the Personal and Business
accounts and provide links to each controller's index action.

It seems like there may be a better way. Any help is much appreciated.

-pachl

Reply

The easiest way to do this is probably to create new controllers normally for Business and Personal and set up routes to make them function hierarchically. For instance, you might say:

map.connect 'new_account/business/:action/:id', :controller => 'business'
map.connect 'new_account/personal/:action/:id', :controller => 'personal'

Alternately, you could write a method has_children that you call in NewAccountController, that takes the names of the child controllers and dynamically generates methods that will render the correct pages.

-Nex3