【Rails】ルーティングのサンプルメモ

# コード

```ruby
  get '/users', to: 'users#index'
  get '/diaries', to: 'diaries#index'
  get '/test', to: 'users#hoge'
```

```
  users GET  /users(.:format)   users#index
diaries GET  /diaries(.:format) diaries#index
   test GET  /test(.:format)    users#hoge
```

# リソースベース（複数形）

```
  resources :test2
```

```
     Prefix Verb   URI Pattern               Controller#Action
test2_index GET    /test2(.:format)          test2#index
            POST   /test2(.:format)          test2#create
  new_test2 GET    /test2/new(.:format)      test2#new
 edit_test2 GET    /test2/:id/edit(.:format) test2#edit
      test2 GET    /test2/:id(.:format)      test2#show
            PATCH  /test2/:id(.:format)      test2#update
            PUT    /test2/:id(.:format)      test2#update
            DELETE /test2/:id(.:format)      test2#destroy
```

```
  resources :test2
  resources :test3, :test4
```

```
     Prefix Verb   URI Pattern               Controller#Action
test2_index GET    /test2(.:format)          test2#index
            POST   /test2(.:format)          test2#create
  new_test2 GET    /test2/new(.:format)      test2#new
 edit_test2 GET    /test2/:id/edit(.:format) test2#edit
      test2 GET    /test2/:id(.:format)      test2#show
            PATCH  /test2/:id(.:format)      test2#update
            PUT    /test2/:id(.:format)      test2#update
            DELETE /test2/:id(.:format)      test2#destroy
test3_index GET    /test3(.:format)          test3#index
            POST   /test3(.:format)          test3#create
  new_test3 GET    /test3/new(.:format)      test3#new
 edit_test3 GET    /test3/:id/edit(.:format) test3#edit
      test3 GET    /test3/:id(.:format)      test3#show
            PATCH  /test3/:id(.:format)      test3#update
            PUT    /test3/:id(.:format)      test3#update
            DELETE /test3/:id(.:format)      test3#destroy
test4_index GET    /test4(.:format)          test4#index
            POST   /test4(.:format)          test4#create
  new_test4 GET    /test4/new(.:format)      test4#new
 edit_test4 GET    /test4/:id/edit(.:format) test4#edit
      test4 GET    /test4/:id(.:format)      test4#show
            PATCH  /test4/:id(.:format)      test4#update
            PUT    /test4/:id(.:format)      test4#update
            DELETE /test4/:id(.:format)      test4#destroy
```

# リソースベース（単数形）

```
  resource :profile
```

```
 new_profile GET    /profile/new(.:format)    profiles#new
edit_profile GET    /profile/edit(.:format)   profiles#edit
     profile GET    /profile(.:format)        profiles#show
             PATCH  /profile(.:format)        profiles#update
             PUT    /profile(.:format)        profiles#update
             DELETE /profile(.:format)        profiles#destroy
             POST   /profile(.:format)        profiles#create
```

# 名前空間

```
  namespace :admin do
    resources :articles
  end
```

```
    admin_articles GET    /admin/articles(.:format)          admin/articles#index
                   POST   /admin/articles(.:format)          admin/articles#create
 new_admin_article GET    /admin/articles/new(.:format)      admin/articles#new
edit_admin_article GET    /admin/articles/:id/edit(.:format) admin/articles#edit
     admin_article GET    /admin/articles/:id(.:format)      admin/articles#show
                   PATCH  /admin/articles/:id(.:format)      admin/articles#update
                   PUT    /admin/articles/:id(.:format)      admin/articles#update
                   DELETE /admin/articles/:id(.:format)      admin/articles#destroy
```

# 名前空間（scope）

```
  scope '/admin2' do
    resources :articles
  end
  resources :articles, path: 'admin3/articles'
  resources :articles, path: '/my-articles'
```

```
          articles GET    /admin2/articles(.:format)          articles#index
                   POST   /admin2/articles(.:format)          articles#create
       new_article GET    /admin2/articles/new(.:format)      articles#new
      edit_article GET    /admin2/articles/:id/edit(.:format) articles#edit
           article GET    /admin2/articles/:id(.:format)      articles#show
                   PATCH  /admin2/articles/:id(.:format)      articles#update
                   PUT    /admin2/articles/:id(.:format)      articles#update
                   DELETE /admin2/articles/:id(.:format)      articles#destroy
                   GET    /admin3/articles(.:format)          articles#index
                   POST   /admin3/articles(.:format)          articles#create
                   GET    /admin3/articles/new(.:format)      articles#new
                   GET    /admin3/articles/:id/edit(.:format) articles#edit
                   GET    /admin3/articles/:id(.:format)      articles#show
                   PATCH  /admin3/articles/:id(.:format)      articles#update
                   PUT    /admin3/articles/:id(.:format)      articles#update
                   DELETE /admin3/articles/:id(.:format)      articles#destroy
                   GET    /my-articles(.:format)                       articles#index
                   POST   /my-articles(.:format)                       articles#create
                   GET    /my-articles/new(.:format)                   articles#new
                   GET    /my-articles/:id/edit(.:format)              articles#edit
                   GET    /my-articles/:id(.:format)                   articles#show
                   PATCH  /my-articles/:id(.:format)                   articles#update
                   PUT    /my-articles/:id(.:format)                   articles#update
                   DELETE /my-articles/:id(.:format)                   articles#destroy

```

# 親子

```
  resources :authors do
    resources :books
  end
```

```
      author_books GET    /authors/:author_id/books(.:format)          books#index
                   POST   /authors/:author_id/books(.:format)          books#create
   new_author_book GET    /authors/:author_id/books/new(.:format)      books#new
  edit_author_book GET    /authors/:author_id/books/:id/edit(.:format) books#edit
       author_book GET    /authors/:author_id/books/:id(.:format)      books#show
                   PATCH  /authors/:author_id/books/:id(.:format)      books#update
                   PUT    /authors/:author_id/books/:id(.:format)      books#update
                   DELETE /authors/:author_id/books/:id(.:format)      books#destroy
           authors GET    /authors(.:format)                           authors#index
                   POST   /authors(.:format)                           authors#create
        new_author GET    /authors/new(.:format)                       authors#new
       edit_author GET    /authors/:id/edit(.:format)                  authors#edit
            author GET    /authors/:id(.:format)                       authors#show
                   PATCH  /authors/:id(.:format)                       authors#update
                   PUT    /authors/:id(.:format)                       authors#update
                   DELETE /authors/:id(.:format)                       authors#destroy
```

# ルーティングの制限

```
  resources :test5, only: [:index, :update]
```


```
       test5_index GET    /test5(.:format)                             test5#index
             test5 PATCH  /test5/:id(.:format)                         test5#update
                   PUT    /test5/:id(.:format)                         test5#update
```

# 参考
Rails のルーティング | Rails ガイド https://railsguides.jp/routing.html