Kaplan Ilya
Kaplan Ilya

Reputation: 693

Rails 3 non-resourceful namespaced route

I noticed that

namespace :admin do
  namespace :manage do
    get 'list'
  end
end

Actually successfully calls following action (for /admin/manage/list):

class Admin::ManageController
  def list
    render :text => 'success'
  end
end

Which is kind of intuitive (that's why i tried it), but it's not covered anywhere in http://guides.rubyonrails.org/routing.html

Can someone tell for sure that it's standard expected functionality that won't stop working after next version or something?

Upvotes: 1

Views: 255

Answers (1)

tihom
tihom

Reputation: 8003

It is not unexpected and is designed to work like that. However, a more typical way of doing it would be

namespace :admin do
  resources :manage do
     collection do
       get 'list'
     end
  end
end

The main difference between namespace and resources is that the latter, by default, provides the standard routes for CRUD actions. Both of them route to Admin::ManageController.

They have similar options as well. See the docs for namespace and resources

If Manage is a resource that can be created and destroyed in your app, it makes more sense to use resources. If it is just an identifier to separate routes for certain actions then use namespace.

Upvotes: 1

Related Questions