Episodes

Resources

Rails Guides - https://guides.rubyonrails.org/routing.html

This episode is sponsored by Honeybadger

Summary

# Terminal
rails routes -g welcome
rails routes -g DELETE
rails routes -g companies

resources :companies

      Prefix Verb   URI Pattern                   Controller#Action
   companies GET    /companies(.:format)          companies#index
             POST   /companies(.:format)          companies#create
 new_company GET    /companies/new(.:format)      companies#new
edit_company GET    /companies/:id/edit(.:format) companies#edit
     company GET    /companies/:id(.:format)      companies#show
             PATCH  /companies/:id(.:format)      companies#update
             PUT    /companies/:id(.:format)      companies#update
             DELETE /companies/:id(.:format)      companies#destroy

resources :companies, only: [:index, :show]

   Prefix Verb URI Pattern              Controller#Action
companies GET  /companies(.:format)     companies#index
  company GET  /companies/:id(.:format) companies#show

resources :companies do
  resource :activates
end

                Prefix Verb   URI Pattern                                     Controller#Action
 new_company_activates GET    /companies/:company_id/activates/new(.:format)  activates#new
edit_company_activates GET    /companies/:company_id/activates/edit(.:format) activates#edit
     company_activates GET    /companies/:company_id/activates(.:format)      activates#show
                       PATCH  /companies/:company_id/activates(.:format)      activates#update
                       PUT    /companies/:company_id/activates(.:format)      activates#update
                       DELETE /companies/:company_id/activates(.:format)      activates#destroy
                       POST   /companies/:company_id/activates(.:format)      activates#create

scope :companies do
  resources :users
end
# resources :users, path: '/companies/users'

   Prefix Verb   URI Pattern                         Controller#Action
    users GET    /companies/users(.:format)          users#index
          POST   /companies/users(.:format)          users#create
 new_user GET    /companies/users/new(.:format)      users#new
edit_user GET    /companies/users/:id/edit(.:format) users#edit
     user GET    /companies/users/:id(.:format)      users#show
          PATCH  /companies/users/:id(.:format)      users#update
          PUT    /companies/users/:id(.:format)      users#update
          DELETE /companies/users/:id(.:format)      users#destroy

scope module: :companies do
  resources :users
end
# resources :users, module: :companies

   Prefix Verb   URI Pattern               Controller#Action
    users GET    /users(.:format)          companies/users#index
          POST   /users(.:format)          companies/users#create
 new_user GET    /users/new(.:format)      companies/users#new
edit_user GET    /users/:id/edit(.:format) companies/users#edit
     user GET    /users/:id(.:format)      companies/users#show
          PATCH  /users/:id(.:format)      companies/users#update
          PUT    /users/:id(.:format)      companies/users#update
          DELETE /users/:id(.:format)      companies/users#destroy

namespace :companies do
  resources :users
end

             Prefix Verb   URI Pattern                         Controller#Action
    companies_users GET    /companies/users(.:format)          companies/users#index
                    POST   /companies/users(.:format)          companies/users#create
 new_companies_user GET    /companies/users/new(.:format)      companies/users#new
edit_companies_user GET    /companies/users/:id/edit(.:format) companies/users#edit
     companies_user GET    /companies/users/:id(.:format)      companies/users#show
                    PATCH  /companies/users/:id(.:format)      companies/users#update
                    PUT    /companies/users/:id(.:format)      companies/users#update
                    DELETE /companies/users/:id(.:format)      companies/users#destroy

resources :companies do
  # resources :users, only: [:index, :new, :create]
  resources :users, shallow: true
end
# resources :users, only: [:show, :edit, :update, :destroy]

          Prefix Verb   URI Pattern                                Controller#Action
   company_users GET    /companies/:company_id/users(.:format)     users#index
                 POST   /companies/:company_id/users(.:format)     users#create
new_company_user GET    /companies/:company_id/users/new(.:format) users#new
       edit_user GET    /users/:id/edit(.:format)                  users#edit
            user GET    /users/:id(.:format)                       users#show
                 PATCH  /users/:id(.:format)                       users#update
                 PUT    /users/:id(.:format)                       users#update
                 DELETE /users/:id(.:format)                       users#destroy

draw(:companies)

# config/routes/companies.rb
resources :companies do
  resources :users, shallow: true
end

concern :commentable do
  resources :comments
end
resources :post, concerns: :commentable

           Prefix Verb   URI Pattern                                Controller#Action
    post_comments GET    /post/:post_id/comments(.:format)          comments#index
                  POST   /post/:post_id/comments(.:format)          comments#create
 new_post_comment GET    /post/:post_id/comments/new(.:format)      comments#new
edit_post_comment GET    /post/:post_id/comments/:id/edit(.:format) comments#edit
     post_comment GET    /post/:post_id/comments/:id(.:format)      comments#show
                  PATCH  /post/:post_id/comments/:id(.:format)      comments#update
                  PUT    /post/:post_id/comments/:id(.:format)      comments#update
                  DELETE /post/:post_id/comments/:id(.:format)      comments#destroy

resources :companies do
  member do
    get :activate
  end
  # get :activate, on: :member
end

          Prefix Verb URI Pattern                       Controller#Action
activate_company GET  /companies/:id/activate(.:format) companies#activate

resources :companies do
  collection do
    get :search
  end
  # get :search, on: :collection
end

          Prefix Verb URI Pattern                 Controller#Action
search_companies GET  /companies/search(.:format) companies#search

resources :companies do
  get 'preview', on: :new
end

             Prefix Verb URI Pattern                      Controller#Action
preview_new_company GET  /companies/new/preview(.:format) companies#preview