difference between collection route and member route in ruby on rails?

What is the difference between collection routes and member routes in Rails?

For example,

resources :photos do
  member do
    get :preview
  end
end

versus

resources :photos do
  collection do
    get :search
  end
end

I don't understand.


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. Search is an example of a collection route, because it acts on (and displays) a collection of objects.


                URL                 Helper                      Description
----------------------------------------------------------------------------------------------------------------------------------
member          /photos/1/preview   preview_photo_path(photo)   Acts on a specific resource so required id (preview specific photo)
collection      /photos/search      search_photos_path          Acts on collection of resources(display all photos)

Theo's answer is correct. For documentation's sake, I'd like to also note that the two will generate different path helpers.

member {get 'preview'} will generate:

preview_photo_path(@photo) # /photos/1/preview

collection {get 'search'} will generate:

search_photos_path # /photos/search

Note plurality!


1) :collection - Add named routes for other actions that operate on the collection. Takes a hash of #{action} => #{method}, where method is :get/:post/:put/:delete, an array of any of the previous, or :any if the method does not matter. These routes map to a URL like /users/customers_list, with a route of customers_list_users_url.

map.resources :users, :collection => { :customers_list=> :get }

2) :member - Same as :collection, but for actions that operate on a specific member.

map.resources :users, :member => { :inactive=> :post }

it treated as /users/1;inactive=> [:action => 'inactive', :id => 1]