resources & resource

resources Resources is so frequent that it is almost always done when routing with Ruby on Rails. resources will automatically route the 7 basic methods in your application to your model.

For example, if you describe the routing using resources for the User model

config/routes.rb


Rails.application.routes.draw do
  resources :users
end

If you display the routes with $ rails routes, it will be as follows.

$ rails routes
   Prefix Verb   URI Pattern               Controller#Action
    users GET    /users(.:format)                    users#index                                               
          POST   /users(.:format)                    users#create                                                     
 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                                           
          DELETE /users/:id(.:format)              users#destroy

In this way, you can easily route with just one line.

** resources are basically used when the model has multiple resources. ** ** For example, the User model will contain many models of users for your application. In addition, the Post model, which is the user's post model, can also use resources because there are multiple posts in the application.

resource But what if you want to route to a single resource instead of to multiple resources?

A singular resource is a resource ** that is used only by the logged-in user on a page within the application **. For example, you can only use the profile page in the Instagram settings, right? Others will not be able to access your profile settings page. (If possible, the account has been hijacked lol)

Use the singular resource instead of resources when configuring routing for such a single resource. For example, let's say you want to route your profile screen.

config/routes.rb


Rails.application.routes.draw do
  resources :users
  #Postscript
  resource :profile
end
$ rails routes
      Prefix Verb   URI Pattern               Controller#Action
 new_profile GET    /profile/new(.:format)             profiles#new
edit_profile GET    /profile/edit(.:format)            profiles#edit
     profile GET    /profile(.:format)                 profiles#show
             PATCH  /profile(.:format)                 profiles#update
             DELETE /profile(.:format)                 profiles#destroy
             POST   /profile(.:format)                 profiles#create

Something is different from resources ,.

** ① No index action **

why? You might think, but this is easy if you think about it.

As mentioned above, I explained that when using resource, a single resource is used. However, the index action is intended to display all the resources of the model. As a result, when dealing with a profile screen that is a single resource, the index action that displays multiple is unnecessary.

** ②: No id ** The: id part has disappeared from the four actions (edit, show, update, destroy).

In fact, this is also the same reason why there is no index action.

The reason : id was needed for the four actions in resources is to specify which of the multiple resources it is. For example, if you edit your own post, you have to specify which id the post is, or you won't know which post to edit. Similarly, when viewing the details of a post, updating a post, or deleting a post, you must specify which post it is.

But what about the profile settings screen? It is a mechanism that you can understand because there is only one profile that is a single resource even if you do not specify it. So the : id disappears because you don't have to specify the id in the first place.

Recommended Posts

resources & resource
Basic Programming Resources