More Routes

same destination but a diff journey...

·

1 min read

Hello children!

If you had read the Rails Routing documentation,

Rails Routing

you would've noticed the :member & :collection routing.

I did not understand 0.001% cos idk.

Thankfully, google came to rescue and launched me to this article here:

Collection vs Member Routes

Thanks to whoever posted it! TL;DR

Lemme explain it in short in my own language here.

Member

Just like the name :member route follows the member of a collection with :id, for example: @post[:id] member of Post.all collection. A simple example provided in Rails Guides shown below.

resources :photos do
  member do
    get 'preview'
  end
end

This will recognise /photos/1/preview with GET, and route to the preview action of PhotosController.

Collection

As you already know, :collection routes boots to the Collection of "objects". As explained in Rails Guides,

resources :photos do
  collection do
    get 'search'
  end
end

This will enable Rails to recognise paths such as /photos/search with GET, and route to the search action of PhotosController where you'll be able to search from a collection of Photo.all.

I'll show you a working example of these routes in details using a simple Inventory Management application I built in the next article. That's it for today.

Thank you all for checking in!

Love,

M.

liked_the_post == true ? comment and share : comment and share