rails 技術ブログ

rails 技術ブログ

勉強したことをアウトプットしていきます

rails routesコマンド

$ rails routesコマンドを実行すると何が確認できるか?

私の手元で実行してみた結果がこちらです↓

            Prefix        Verb          URI   Pattern                               Controller#Action
        letter_opener_web        /letter_opener                                   LetterOpenerWeb::Engine
                     root GET    /                                                static_pages#top
                    login GET    /login(.:format)                                 user_sessions#new
                          POST   /login(.:format)                                 user_sessions#create
                   logout DELETE /logout(.:format)                                user_sessions#destroy
          password_resets POST   /password_resets(.:format)                       password_resets#create
       new_password_reset GET    /password_resets/new(.:format)                   password_resets#new
      edit_password_reset GET    /password_resets/:id/edit(.:format)              password_resets#edit
           password_reset PATCH  /password_resets/:id(.:format)                   password_resets#update
                          PUT    /password_resets/:id(.:format)                   password_resets#update
             edit_profile GET    /profile/edit(.:format)                          profiles#edit
                  profile GET    /profile(.:format)                               profiles#show
                          PATCH  /profile(.:format)                               profiles#update
                          PUT    /profile(.:format)                               profiles#update
                    users POST   /users(.:format)                                 users#create
                 new_user GET    /users/new(.:format)                             users#new
           board_comments POST   /boards/:board_id/comments(.:format)             comments#create
             edit_comment GET    /comments/:id/edit(.:format)                     comments#edit
                  comment PATCH  /comments/:id(.:format)                          comments#update
                          PUT    /comments/:id(.:format)                          comments#update
                          DELETE /comments/:id(.:format)                          comments#destroy

それぞれ何を表しているのか説明すると…

Prefix
後ろに_pathと付けてlink_toなどで使うとそのページに飛ぶことができる
Verb
HTTPメソッドの種類
URI Pattern
ルーティングのパスのこと
Controller#Action
対応しているのはどのコントローラのどのアクションかが記載されている

-gオプション、-cオプション

$ rails routes -g < URLヘルパー名、HTTP動詞、URLパスのいずれか >
URLヘルパー名、HTTP動詞、URLパスのいずれかに部分マッチするルーティングが出力される

rails routes -g root_path
rails routes -g GET


`rails routes -c < コントローラ名 >
指定したコントローラに対応するルーティングだけ出力される

$ rails routes -c users



■参考にした記事

【Rails】rake routesコマンドの使い方をマスターしよう! | Pikawaka - ピカ1わかりやすいプログラミング用語サイト