Search code examples
ruby-on-railsrubydevisenested-routes

nested resources in devise


I am new to rails and I would like some advice as on my routes and the proper routing logic. I am working on a very simple app where users can post lists. The user (devise model) has_many lists and the list belongs_to a user. My list table has a user_id:integer in it. When the user successfully logins then I would like them to see their lists on their corresponding route page. I created the nested routes like so:

devise_for :users, :paths => 'users'

resource :users do
  resource :lists
end

This is the output from my rake routes

new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
             users_lists POST   /users/lists(.:format)         lists#create
         new_users_lists GET    /users/lists/new(.:format)     lists#new
        edit_users_lists GET    /users/lists/edit(.:format)    lists#edit
                         GET    /users/lists(.:format)         lists#show
                         PATCH  /users/lists(.:format)         lists#update
                         PUT    /users/lists(.:format)         lists#update
                         DELETE /users/lists(.:format)         lists#destroy
                   users POST   /users(.:format)               users#create
               new_users GET    /users/new(.:format)           users#new
              edit_users GET    /users/edit(.:format)          users#edit
                         GET    /users(.:format)               users#show
                         PATCH  /users(.:format)               users#update
                         PUT    /users(.:format)               users#update
                         DELETE /users(.:format)               users#destroy
                    root GET    /                              static_pages#home

How can I have my routes achieve this:

/users/:user_id/lists(.:format)

It would also be a stepping point to make sure that the user has access only to his lists.


Solution

  • You probably want to pluralize the list resource and make the user resource singular:

    resource :user do
      resources :lists
    end
    

    For part two, just load the lists based on the current user where you need them

    @lists = current_user.lists