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:

On Wed, Mar 17, 2010 at 3:14 PM, Stefan B. [email protected]
wrote:

in my routes.rb I add:

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 T. 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.

On Wed, Mar 17, 2010 at 4:34 PM, Stefan B. [email protected]
wrote:

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

On Wed, Mar 17, 2010 at 4:34 PM, Stefan B. [email protected]
wrote:

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

Sent from my iPhone

On Mar 17, 2010, at 5:43 PM, Stefan B. [email protected] wrote:

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 T. 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 T. 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 B. wrote in post #897683:

Conrad T. 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.