1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

railsの「resources」と「resource」の違いについて

Posted at

#はじめに「resources」メソッドとは何か
railsアプリで登場する7つのアクションを自動で設定してくれる便利なメソッドです。
「routes.rb」で1行記述するだけでアプリケーションに必要なルーティングが自動生成されます。

「resources」メソッドには単数形の「resource」も存在します。
この違いについて以下解説していきます。

#複数形の 「resources」
複数形の 「resources」は、複数のリソースに対する処理を行うためのルーティングを生成してくれます。
簡単に説明すると、urlに「/:id」が含まれた状態で生成されます。

例えば以下のように「resources」で「users」コントローラーのアクションを生成したとします。

routes.rb
resources :users, only: [:show, :index, :new, :edit, :update]
                  #onlyでアクションを絞っています

ターミナルにて「rails routes」を実行してみます。

$ rails routes
                          〜 省略 〜

          users GET    /users(.:format)             users#index                                          
       new_user GET    /users/new(.:format)         users#new                                                                 
      edit_user GET    /users/:id/edit(.:format)    users#edit                                                             
           user GET    /users/:id(.:format)         users#show                                                                   
                PATCH  /users/:id(.:format)         users#update                                                                  
                PUT    /users/:id(.:format)         users#update                                                                 

                          〜 省略 〜

urlに「/:id」が含まれています。
urlに「/:id」を含むことで、それぞれのリソース(user)ごとに処理を行うことができます。

#単数形の「resource」
単数形の「resource」は1つのリソースに対する処理を行うためのルーティングを生成してくれます。
こちらも以下の通り「resource」で「users」コントローラーのアクションを生成したとします。
(単数形の「resource」を使用する場合はコントローラー名も単数形で記載します。)

routes.rb
resource :user, only: [:show, :index, :new, :edit, :update]
                  #onlyでアクションを絞っています

ターミナルにて「rails routes」を実行してみます。

$ rails routes
                            〜 省略 〜

         new_user GET    /user/new(.:format)     users#new                                                            
        edit_user GET    /user/edit(.:format)    users#edit                                                                
             user GET    /user(.:format)         users#show                                                                    
                  PATCH  /user(.:format)       users#update                                                                      
                  PUT    /user(.:format)         users#update

                            〜 省略 〜                                                                      

urlに「/:id」が含まれてないですね。
また、「/:id」が無いので「:index」アクションも生成されません。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?