Included resources in development
I'm new to rails and I would like some advice on my routes and correct routing logic. I am working on a very simple application where users can post lists. The user (modeler) has multiple lists, and the list is owned by the user. My list table has user_id: integer. When the user successfully logs in, I would like them to see their lists on the corresponding route page I created nested routes like this:
devise_for :users, :paths => 'users'
resource :users do
resource :lists
end
This is the result of 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 achieve my routes:
/users/:user_id/lists(.:format)
This would also be a step to make sure the user only has access to their lists.
You probably want to split the list resources and make the user resource unique:
resource :user do
resources :lists
end
For the second part, just load the lists based on the current user wherever you need them.
@lists = current_user.lists
You should use the resources
method instead resource
:
resources :users do
resources :lists
end
Take a look at this Rails Guides . It contains a good example with explanations.
And for this:
This would also be a step to make sure the user only has access to their lists.
You can use one of the design methods of support : current_user
:
current_user.lists
UPD: But I think it's easier to create lists than a separate (not nested) resource. Something like that:
# 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, along the way, /lists
some user will only have access to their own lists. What is it.
I think you can do this:
resources :users do
resources :lists
end
Pluralization is the key.
There is a link to this part of the docs here
I would use an authorization like cancancan or pundit to control which lists the user can or cannot see. Hope it helps!