capcode01
capcode01

Reputation: 163

Rails 3 create a route to nested independent page

I have 3 levels of nesting.

routes.rb looks like this

    resources :clients do
       resources :departments do
         resources :tasks
       end
    end

I would like to create a custom path that looks like this

/clients/:client_id/departments/:department_id/tasks/data

I have tried adding the following

    resources :clients do
       resources :departments do
         resources :tasks
           member do
             get "data"
           end
       end
    end

This creates the route

/clients/:client_id/departments/:department_id/tasks/:task_id/data

How would I remove the :task_id part the path?

Upvotes: 0

Views: 60

Answers (2)

siekfried
siekfried

Reputation: 2964

A member route acts on a member, that's why it requires an id. A collection acts on a collection and so doesn't require an id.

resources :clients do
   resources :departments do
     resources :tasks do
       collection do
         get "data"
       end
     end
   end
end

Upvotes: 3

Manoj Monga
Manoj Monga

Reputation: 3093

You should use

resources :clients do
   resources :departments do
     resources :tasks
       get "data", :on => :collection
   end
end

Upvotes: 1

Related Questions