[RUBY] [Rails] I learned about the difference between resources and resources

I'm a beginner with "super" who is struggling with Rails every day. I read it in the text a few weeks ago, and if I use resources for routing, I don't know what it is, but I remember that the action and HTTP method are tied together, and I kept typing resources without knowing the reason.

Of course, there was a moment when I got stuck in such a way & understanding, so I thought I would sit down and face resources (plural) and resources (singular), so I wrote this article.

What are resources (plural) in the first place?

A resource is a row of information stored in a database table. It is a super convenient function that allows you to create a "RESTful" app by defining multiple routes just by writing resources in one line.

If you didn't use resources

routes.rb


Rails.application.routes.draw do
  get 'tweets'     => 'tweets#index'
  get 'tweets/:id' => 'tweets#show'
  get 'tweets/new' => 'tweets#new'
  post 'tweets' => 'tweets#create'
  get 'tweets/:id/edit' => 'tweets#edit'
  patch 'tweets/:id'  => 'tweets#update'
  delete 'tweets/:id' => 'tweets#destroy'
end

I have to manually enter all the descriptions, which is very confusing. With this alone, it seems like it will collapse for about half a day.

So, using the rumored resources, I tried to describe the routing,

routes.rb


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

High end. When I check the terminal,

Terminal


tweets     GET    /tweets(.:format)          tweets#index
           POST   /tweets(.:format)          tweets#create
new_tweet  GET    /tweets/new(.:format)      tweets#new
edit_tweet GET    /tweets/:id/edit(.:format) tweets#edit
tweet      GET    /tweets/:id(.:format)      tweets#show
           PATCH  /tweets/:id(.:format)      tweets#update
           PUT    /tweets/:id(.:format)      tweets#update
           DELETE /tweets/:id(.:format)      tweets#destroy

Isn't the routing for performing CRUD processing completed brilliantly? This is convenient.

Then what is resource (singular)?

Now here's the guy who bothers me. Actually, there is also a method called resource that describes not only the plural form but also the singular form. Let's check it anyway.

Actually, if you look at the contents of routing using resources,

routes.rb


Rails.application.routes.draw do
  resource :tweet #resource(Singular)に合わせて、モデル名もtweetとSingularにしています!
end

At first glance, the routing looks almost the same. However, if you look at the terminal, you'll notice that there are some differences from when you wrote the resources (plural).

Terminal


new_tweet  GET    /tweet/new(.:format)   tweets#new
edit_tweet GET    /tweet/edit(.:format)  tweets#edit
tweet      GET    /tweet(.:format)       tweets#show
           PATCH  /tweet(.:format)       tweets#update
           PUT    /tweet(.:format)       tweets#update
           DELETE /tweet(.:format)       tweets#destroy
           POST   /tweet(.:format)       tweets#create

Differences between resources and resources

There are two major differences, ① Is there an index action? ② Whether to include: id in url </ strong> is.

Tweets need a list feature, so if you want to use it, it should be resources. However, for things that don't need to be listed by index, such as a user's profile, it's better to use resource.

In actual operation, I think it is difficult to use them simply, but to put it simply, In the case of multiple resources existing on the application → resources If there is only one resource on the app → resource I think it's okay to understand like this.

member and collection

Now you know the difference between resources and resources, it's 100 people! </ strong> It is 20 million years early to be smart. The only actions that can be defined using resources are the seven main actions listed above.

"I want to add a follow function, but ..." "I wonder if the search function is impossible ..." If you want to develop functions from 7 basic actions such as, more convenient functions "member" and "collection" </ strong> there is.

What is a member?

Imagine implementing a user-to-user follow-up feature. Of course, the User model itself can also use resources to define routing (because there are multiple users on the app).

Identify the user's ids, associate them, and set actions for the individual resources specified by the ids!

routes.rb


Rails.application.routes.draw do
  resources :users do
    get :follows, on: :member
    get :followers, on: :member
  end
end

Hi this is all. When I check the terminal,

Terminal


follows_user    GET   /users/:id/follows(.:format)    users#follows
followers_user  GET   /users/:id/followers(.:format)  users#followers
#Below, 7 actions are omitted

Stunningly, you can set follows and followers actions for individual resources, including the user's id!

What is a collection?

Then, move on to the collection as it is. The difference with member is that you can set actions for the entire resource. Let's explain in detail.

As for routing,

routes.rb


Rails.application.routes.draw do
  resources :users do
    get :search, on: :collection
  end
end

It has become an annual event. Let's check the terminal.

Terminal


search_users   GET    /users/search(.:format)    users#search
#Below, 7 actions are omitted

If you use collection, you can route to the search action without the: id in the url.

in conclusion

Thank you for your long and uncoordinated explanation. While explaining myself, there was a part that deepened my understanding, so I think that the more I use it, the more it will permeate my body. I hope this will help you study programming.

Recommended Posts