0
votes

I'm migrating an application from Rails 2 to Rails 3.
We have multiple namespaced controllers with different namespaces. As they are not RESTfull I don't want to use resource routing, instead I would like to have an old Rails 2 like match ':controller/:action' that picks up namespaced controllers.

In my routes.rb I have

# Install the default route as the lowest priority.
match ':controller(/:action(/:id(.:format)))'
match ':controller(/:action(/:id(.:format)))', :controller => /[^\/]+\/[^\/]+/

rake routes reports

/:controller(/:action(/:id(.:format)))          :controller#:action
/:controller(/:action(/:id(.:format)))          (?-mix:[^\/]+\/[^\/]+)#:action

Still a request to /config/companies/index fails

ActionController::RoutingError (No route matches [GET] "/config/companies/index"):

What am I doing wrong? Is there another way to get namespaced routes with dynamic segments? When I try to use namespace and a match with a dynamic segment together it throws an error.

:controller segment is not allowed within a namespace block
1

1 Answers

0
votes

OK I found the problem.
Config is a reserved constant in Rails, it points to RbConfig. My match condition actually works but tries to call RbConfig::CompaniesController which of course does not exist.

When I tried to add

match '/:controller(/:action(/:id(.:format)))', :controller => /config\/[^\/]+/

the error was

ActionController::RoutingError (uninitialized constant RbConfig::CompaniesController)

Solution: rename the app/controllers/config -> app/controllers/configuration (and the views folder) and add a redirect to the routing to handle legacy links.

match '/config/*path' => redirect("/configuration/%{path}")