[Ruby] Explanation of the order of rails routes

less than 1 minute read

Summary for myself

https://qiita.com/gawach/items/4e7460d06f70e3013eea
It is summarized in, but I have a different understanding.
I will explain it.

⑴
  1 get "posts/:id" => "posts#show"
  2 get "posts/new" => "posts#new"
⑵
  3 get "posts/new" => "posts#show"
  4 get "posts/:id" => "posts#new"
⑶ (Correct code written on Progate)
    get "posts/index" => "posts#index"
    get "posts/:id" => "posts#show" 

⑴ is wrong, and ⑵ is actually correct, which is shown below.

Because the code is prioritized from the top
Make sure that certain URLs other than: id come before: id.
And the rest of them are subject to: id.
Because there are rules like the above
If: id is described before URL such as posts / new, an error will occur.