[RUBY] Learn more about collections and members in routes.rb

When do you use collections and members in the first place?

** [Conclusion] ** "When routing is set in resources, the resources method does not generate` automatically ʻaction to action Used when setting up routing.

There are usually seven actions to take when setting up routing in a Rails web application. In many cases, the resources method that is automatically generated is used, but when implementing a new post search function, for example, routing must be generated in addition to the seven actions that can be set in resources. Therefore, use collection and member in such cases.

So what's the difference between collection and member?

** [Conclusion] ** The difference between ** whether to include ** ": id" when configuring routing **.

collection In the routing ":"id" is not attached
member In the routing ":"id" is not attached

** [Example] When defined by collection **

route.rb


Rails.application.routes.draw do
  resources :tweets do
    collection do
      get 'search'
    end
  end
end

** [Example] Collection routing **

Prefix           Verb    URI                                 Pattern
search_tweets    GET    /tweets/search(.:format)              tweets#search

** * Looking at the above, you can see that ": id" is not attached. ** ** In other words, if you don't need to specify the data like the post list page, you can use collection !!

** [Example] When defined by member **

routes.rb


Rails.application.routes.draw do
  resources :tweets do
    member do
      get 'search'
    end
  end
end

** [Example] member routing **

Prefix           Verb    URI                                 Pattern
search_tweet      GET    /tweets/:id/search(.:format)       tweets#search

** * If you look at the above, you can see that ": id" is attached. ** ** In other words, use member when you need to specify the data like the detail page! !!

Summary

collections and members are when using the resources method in the routing settings ** Used when you want to add a new action **. Also, if you want to receive ** ": id" ** in params when moving that action, use member and Use collection to add an action, especially if you don't need to go to a specific page with ** id **.

I found the concept of this part that I used when I added the search function myself to be very difficult, I didn't understand it well, but when I looked it up again and learned it, I was able to understand it without any trouble.

********************************************

** Reference article ** I tried to explain the difference between members and collection in routes.rb of rails in an easy-to-understand manner. ~ Rails from beginner to intermediate ~

Recommended Posts

Learn more about collections and members in routes.rb
Learn more about gems and bundlers
Learn more about docker image and Dockerfile FROM
Learn Flyweight patterns and ConcurrentHashMap in Java
About the difference between classes and instances in Ruby