Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails routing - :on => :collection

The Rails routing guide doesn't specify what :on => :collection means.

I cannot find an explanation to what the :on key is, nor what a :collection is in that context.

like image 834
Nick Ginanto Avatar asked Oct 25 '12 08:10

Nick Ginanto


People also ask

What does collection do in Rails routes?

Rails routes on resources support member routing as well as collection routing. Member routes act on a member of the resource. Collection routes acts on resources in general.

What are collection routes?

Collection Route means a route established by the designated officer, covering an area within which waste will be collected by the city.

What is the difference between collection route and member routes?

Considering the same case, the two terms can be differentiated in a simple way as :member is used when a route has a unique field :id or :slug and :collection is used when there is no unique field required in the route.

What is difference between collection and member in Rails?

A member route will require an ID, because it acts on a member. A collection route doesn't because it acts on a collection of objects. Preview is an example of a member route, because it acts on (and displays) a single object.


1 Answers

Routes on collections are listed here.

The difference between :on => :collection and :on => :member are the style of route they produce and their associated route helpers.

resources :posts do   # on collection   get 'search', on: :collection    # --> generates '/posts/search' and search_posts_path    # on member   get 'share', on: :member         # --> generates'/posts/:id/share' and share_photo_path(@post) end 
like image 115
Thomas Klemm Avatar answered Sep 19 '22 18:09

Thomas Klemm