Ruby on Rails Member and Collection Routes


Example

Defining a member block inside a resource creates a route that can act on an individual member of that resource-based route:

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

This generates the following member route:

get '/posts/:id/preview', to: 'posts#preview'
# preview_post_path

Collection routes allow for creating routes that can act on a collection of resource objects:

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

This generates the following collection route:

get '/posts/search', to: 'posts#search'
# search_posts_path

An alternate syntax:

resources :posts do
  get 'preview', on: :member
  get 'search',  on: :collection
end