[Ruby] Explanation of the order of rails routes

less than 1 minute read

Summary for myself

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.