LoginSignup
7
7

More than 3 years have passed since last update.

rails routes(ルーティング)の順番の解説

Posted at

自分用のまとめ

https://qiita.com/gawach/items/4e7460d06f70e3013eea
にてまとめられているが、私は違う理解の仕方をしている。
それを説明します。

⑴
  1 get "posts/:id" => "posts#show"
  2 get "posts/new" => "posts#new"
⑵
  3 get "posts/new" => "posts#show"
  4 get "posts/:id" => "posts#new"
⑶(プロゲートに書いてあった正しいコード)
    get "posts/index" => "posts#index"
    get "posts/:id" => "posts#show" 

⑴は間違いであり、⑵は実は正しいと決める、それを下記に示す。

上から順にコードを優先していくため、
:id以外の特定のURLが:idより先にくるようにする。
そしてそれら以外の余ったものが:idの対象となる。
上記のようなルールがあるため、
:idはposts/newのようなURLより先に記述されているとエラーになる。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7