Nick Ginanto
Nick Ginanto

Reputation: 32120

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.

Upvotes: 36

Views: 15425

Answers (1)

Thomas Klemm
Thomas Klemm

Reputation: 10856

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

Upvotes: 72

Related Questions