Michael Brooks
Michael Brooks

Reputation: 489

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.

Upvotes: 4

Views: 5513

Answers (3)

schfkt
schfkt

Reputation: 96

You should use resources method instead of resource:

resources :users do
  resources :lists
end

Take a look on this Rails Guides article It contains good example with explanations.

And for this:

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

You can use one of devise's helper methods: current_user:

current_user.lists

UPD: But I think it is easier to create lists as a separate (not nested) resource. Something like this:

# config/routes.rb
Rails.application.routes.draw do
  # ...
  resources :lists
end

# app/controllers/lists_controller.rb
class ListsController < ApplicationController
  before_action :authenticate_user!

  def index
    @lists = current_user.lists
  end
end

So, at the /lists path some user will get access only to his own lists. That's it.

Upvotes: 2

Ross
Ross

Reputation: 326

I think that you can make that work like this:

resources :users do resources :lists end The key being the pluralization.

Here is a link to that part of the docs

I would use authorization like cancancan or pundit to control what lists a user can or can't see. Hope that helps!

Upvotes: 1

NM Pennypacker
NM Pennypacker

Reputation: 6942

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

Upvotes: 1

Related Questions