Joshua
Joshua

Reputation: 805

Nested Routes alternative

I am having some problems with nesting resources. I want to find out if there is a better way of doing things.

I have a to-do list application with three resources user, list and task. Each user has his/her own todo list.

My question is how else can i set up associations and routes to prevent me nesting three layer deep in my route file.

resources :users do
 resources :list do
  resources :task do
  end
 end
end

I want to prevent that. cheers

Upvotes: 1

Views: 368

Answers (2)

Stefan
Stefan

Reputation: 114178

Since a user can only see her/his own lists and tasks, you don't have to nest these resources. Define them separately in your routes file:

resources :users

resources :lists do
  resources :tasks
end

And retrieve the current user from your authentication framework:

class ListsController < ApplicationController

  def index
    @lists = current_user.lists
  end

end

Upvotes: 1

gvalmon
gvalmon

Reputation: 958

Probably duplicate for: Rails 3 level deep nested resources

Try experimenting with :shallow option:

resources :users, shallow: true do 
  resources :lists, shallow: true do
    resources :task
  end
end

Upvotes: 1

Related Questions