LoginSignup
2
1

More than 3 years have passed since last update.

【Rails】ルーティングでネストさせる時 Resources か Resource どちらが良いのか

Last updated at Posted at 2020-10-08

⭐️ Rails、かなりの初学者です。
  シンプルな理解で、同じような方の参考になればと思います。

⭐️状況

 ・簡単な本の投稿アプリ
 ・投稿された本に対して いいね!機能と コメント機能
 ・config/routes.rb内に 
  いいね!(favorite) と コメント(comment) を親であるBookに対してネストしたい
・ favorite と comment は resourcesかresourceどちらを使えばいいのかわからない
 (参考にしていた教材がfavoriteに対してはresourceでcommentに対してはrecourcesを使っていた為、混乱してしまった。)


まずはネストさせる前にresourcesとresourceでパスがどう違うのか確認します。

・ resources

routes.rb
resources :books
ターミナル
     books   GET    /books(.:format)            books#index                                                                    
           POST   /books(.:format)            books#create                  
  new_book   GET    /books/new(.:format)        books#new                                                                  
  edit_book  GET    /books/:id/edit(.:format)   books#edit                                                               
       book  GET    /books/:id(.:format)        books#show                                                           
             PATCH  /books/:id(.:format)        books#update                                                          
             PUT    /books/:id(.:format)        books#update                                                               
             DELETE /books/:id(.:format)        books#destroy   

・ resource

routes.rb
resource :book
ターミナル
 new_book   GET    /book/new(.:format)         books#new                                                                 
 edit_book  GET    /book/edit(.:format)        books#edit                                                                  
 book       GET    /book(.:format)             books#show                                                             
            PATCH  /book(.:format)             books#update                                                        
            PUT    /book(.:format)             books#update                                                         
            DELETE /book(.:format)             books#destroy                                                       
            POST   /book(.:format)             books#create  

上記から確認できるこの2つの違い

:writing_hand:resourcesではパスにidを持たせて、コントローラーにindexアクションが生成されている
:writing_hand:resourceではパスがidを持たず、indexアクションも生成されない

resourcesは複数存在するリソースに対してRESTのルーティングを設計したいときに使う(7種類の異なるアクション)
resourceは1つだけ存在するリソースに対してRESTのルーティング設計をしたいときに使う

もっと具体的なシチュエーション

idが作られないということは
複数のリソースの中から「ある一つのリソース」を特定しなくていいということになります。
(そもそも一つしかリソースがない、なのでindexも不要)

特定をする時は「show画面」や「edit画面」が必要な時なので、
ネストで子の部分をresourcesかresourceで悩んだ時は「show画面」や「edit画面」が必要か?で判断するようにする。

今回の結論

いいね!に対しても、コメントに対してもshow画面などは用意しないアプリだったので、
このようにネストさせました。

routes.rb
  resources :books do
    resource :favorite, only: [:create, :destroy]
    resource :book_comment, only: [:create, :destroy]
  end
ターミナル
book_favorite       DELETE /books/:book_id/favorite(.:format)    favorites#destroy                                                   
                    POST   /books/:book_id/favorite(.:format)    favorites#create                                                     
book_book_comment   DELETE /books/:book_id/book_comment(.:format)  book_comments#destroy                                                 
                    POST   /books/:book_id/book_comment(.:format)  book_comments#create                                                 

まとめ

細かい理解を大切にして、
その時の状況に合わせて上手に組み合わせができるようにしたいと思います:relaxed:

何かご指摘がありましたら、お願い致します。

参考

https://qiita.com/Atsushi_/items/bb22ce67d14ba1abafc5
https://railsguides.jp/routing.html

参考にさせていただきました!
ありがとうございます😊

2
1
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
2
1