1

I've three models Project, Card and Task.

project has_many :cards

card has_many :tasks

I've defined the route for building cards like following:

resources :projects, except: [:new, :edit, :show] do
    resources :cards do
      resources :tasks
    end
end

It'll create path for cards as: projects/:project_id/cards/

It'll create path for tasks as: projects/:project_id/cards/:card_id/tasks

What I need is: Card routes should be nested to Project. (which I currently have) and Task routes should nested to only Card like /cards/:card_id/tasks (which I need).

How can I achieve that?

Thanks in advance!

Javier Menéndez Rizo
  • 2,138
  • 3
  • 12
  • 22
Emu
  • 5,763
  • 3
  • 31
  • 51

2 Answers2

1
resources :projects, except: [:new, :edit, :show] do
    resources :cards
end
resources :cards do
    resources :tasks
end

This is what you are looking for

0

You can just do,

resources :projects, except: [:new, :edit, :show] do
    resources :cards
end

And further try with defining each route for task by,

get '/cards/:card_id/tasks', to: 'tasks#index'

I did not test but should work, Sad is you have to define it for each specific route.

ray
  • 5,454
  • 1
  • 18
  • 40
  • In this way I need to manually add all the task routes like edit, create, update, destroy etc... – Emu Jan 09 '19 at 10:07
  • I wish there is better alternative but to make it work for temporary you can try above. – ray Jan 09 '19 at 10:09