【Rails】resourcesが訳わからなくなる人のためのド基本まとめ

ルーティングする際にかっこつけてresources使うけど、ネストとかしていくうちにprefixもURI patternも段々わけ分からなくなってくる私。毎回ググるのももう疲れたので自分でまとめることにした。似たような人がもし存在するのならばそんな人の役にも多分立つはず。ド初心者のための超基本のまとめなので、shallowとかそういうワンステップ上っぽいことには触れてません。

resources :posts # メイン7つ全て

resources :posts, only: [:◯◯, :△△] # 特定のアクションだけ

resources :posts, except: [:◯◯, :△△] # 特定のアクション以外
prefix verb URI pattern controller#action
posts GET /posts posts#index
new_post GET /posts/new posts#new
posts POST /posts posts#create
post GET /posts/:id posts#show
edit_post GET /posts/:id/edit posts#edit
post PATCH/PUT /posts/:id posts#update
post DELETE /posts/:id posts#destroy
# ネスト

resources :posts do
  resources :comments
end
prefix verb URI pattern controller#action
post_comments GET /posts/:post_id/comments comments#index
new_post_comment GET /posts/:post_id/comments/new comments#new
post_comments POST /posts/:post_id/comments comments#create
post_comment GET /posts/:post_id/comments/:id comments#show
edit_post_comment GET /posts/:post_id/comments/:id/edit comments#edit
post_comment PATCH/PUT /posts/:post_id/comments/:id comments#update
post_comment DELETE /posts/:post_id/comments/:id comments#destroy

indexcreateが同じprefixとURI pattern(HTTPメソッドで送信先を使い分ける)
showupdatedestroyが同じprefixとURI pattern(HTTPメソッドで送信先を使い分ける)


多分慣れてくるとshallowあたりを理解したくなってくるんだと思う。そんな時に読みたいメモ。 resources を nest するときは shallow を使うと幸せになれる - Qiita