Rails 3 Routing: Namespace'd Controllers?

Hi there,

I'm stuck with some very simple routing requirements. Somehow I believe
this is not even possible with the new Rails 3 Router:

So i have my Admin::SettingsController class in controllers/admin

My intention is to access the admin-controllers via
/admin/:controller/:action

in my routes.rb I add:
namespace :admin do
  match ':controller(/:action(/:id))'
end (I want the admin part of my application to be unRESTful)

However, now EVERY controller is accessable via
/admin/controller/action!... For example, url_for :controller =>
'books', :action => 'show' generates "/admin/books/show" ... likewise,
url_for :controller => 'admin/settings', :action => 'group' now
generates "/admin/admin/settings/group" (should be:
"/admin/settings/group".

How can I get SIMPLE namespace'd controllers to work in rails without
affecting other controllers? Does anyone know?

Thank you :slight_smile:

Hi there,

I’m stuck with some very simple routing requirements. Somehow I believe

this is not even possible with the new Rails 3 Router:

So i have my Admin::SettingsController class in controllers/admin

My intention is to access the admin-controllers via

/admin/:controller/:action

in my routes.rb I add:

namespace :admin do

match ‘:controller(/:action(/:id))’

end (I want the admin part of my application to be unRESTful)

However, now EVERY controller is accessable via

/admin/controller/action!.. For example, url_for :controller =>

‘books’, :action => ‘show’ generates “/admin/books/show” … likewise,

url_for :controller => ‘admin/settings’, :action => ‘group’ now

generates “/admin/admin/settings/group” (should be:

“/admin/settings/group”.

How can I get SIMPLE namespace’d controllers to work in rails without

affecting other controllers? Does anyone know?

Thank you :slight_smile:

Stefan, in Rails 3, you can do the following in your routes.rb:

namespace :admin do
resources :settings
end

Then, you’ll end up with the following controllers:

/admin/settings

Good luck,

-Conrad

Conrad Taylor wrote:

namespace :admin do resources :settings end

Then, you'll end up with the following controllers:

/admin/settings

Hey,

thanks for the hint, but resources() only adds the standard actions to
the route (index, show, edit, new, create, update, delete). I'm looking
for a more generic approach using default routes for many controllers
and actions in one namespace.

Conrad Taylor wrote:

namespace :admin do resources :settings end

Then, you’ll end up with the following controllers:

/admin/settings

Hey,

thanks for the hint, but resources() only adds the standard actions to

the route (index, show, edit, new, create, update, delete). I’m looking

for a more generic approach using default routes for many controllers

and actions in one namespace.

Yes, this is correct unless you had another question because it wasn’t

clear from the original e-mail. If you would like to add non-standard action(s)

to a controller, then you’ll need to do the following:

namespace :admin do
resources :settings do

  get :some_action, :on => :member # member route

  get :some_other_action, :on => :collection # collection route

end
end

Good luck,

-Conrad

Conrad Taylor wrote:

namespace :admin do resources :settings end

Then, you’ll end up with the following controllers:

/admin/settings

Hey,

thanks for the hint, but resources() only adds the standard actions to

the route (index, show, edit, new, create, update, delete). I’m looking

for a more generic approach using default routes for many controllers

and actions in one namespace.

Stefan, I would also recommend reading following documentation for

something that meets your requirements:

http://guides.rails.info/routing.html

Good luck,

-Conrad

Conrad Taylor wrote:

namespace :admin do resources :settings do
get :some_action, :on => :member # member route
get :some_other_action, :on => :collection # collection route
end end

Yes, someone *could* explicitly define all actions in the routes.rb, but
that is exactly what I do not want to do -- this is not even close to a
generic solution :stuck_out_tongue:

In addition, I dont like the idea of squeezing all the unRESTful parts
of the application into resources, when I do not even need or want all
the additional RESTful routes.

So, the question still stands: Is there a way to have regular (generic!)
routes in namespaces like in rails 2?

Conrad Taylor wrote:

namespace :admin do resources :settings do
get :some_action, :on => :member # member route
get :some_other_action, :on => :collection # collection route
end end

Yes, someone *could* explicitly define all actions in the routes.rb, but
that is exactly what I do not want to do -- this is not even close to a
generic solution :stuck_out_tongue:

Yes, this is true but you have a much greater level of control over your actions. The default routes will make every action in Rails 3.0 use a GET request. Thus, you'll need to refactor all your views as appropriately.

In addition, I dont like the idea of squeezing all the unRESTful parts
of the application into resources, when I do not even need or want all
the additional RESTful routes.

So, the question still stands: Is there a way to have regular (generic!)
routes in namespaces like in rails 2

Please post the Rails 2.x route that you would like to convert to Rails 3.0?

-Conrad

Conrad Taylor wrote:

Sent from my iPhone

Please post the Rails 2.x route that you would like to convert to
Rails 3.0?

Hey Conrad,

I think I made a mistake.

I was under the impression that following code in rails 2
map.namespace :admin do |admin|
  admin.connect ':controller/:action/:id'
end
was enabling /admin/any_controller/any_action =>
Addmin::AnyController#any_action
but it did not.

instead, the default root was still enabled. The above code never
worked.

Ultimatly, what I am trying to do, is to use the default root but ONLY
for controllers in a specific namespace (other parts of the application
are using resources)

Stefan Buhr wrote in post #897683:

Conrad Taylor wrote:

Sent from my iPhone

Please post the Rails 2.x route that you would like to convert to
Rails 3.0?

Hey Conrad,

I think I made a mistake.

I was under the impression that following code in rails 2
map.namespace :admin do |admin|
  admin.connect ':controller/:action/:id'
end
was enabling /admin/any_controller/any_action =>
Addmin::AnyController#any_action
but it did not.

instead, the default root was still enabled. The above code never
worked.

Ultimatly, what I am trying to do, is to use the default root but ONLY
for controllers in a specific namespace (other parts of the application
are using resources)

What your looking for is scope in routes:
http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

scope "/admin" do
  match ":controller(/:action(/:id))"
  resources :posts # :controller => PostsController
end

I feel that the following do work.

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

In rails3, such a constraint condition for namespaced controllers may be
necessary.