290

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.

ExiRe
  • 4,657
  • 7
  • 46
  • 92
never_had_a_name
  • 85,690
  • 100
  • 264
  • 377

4 Answers4

433

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.

Theo
  • 128,885
  • 20
  • 149
  • 197
  • What about the create action? Say I wanted to make an alternative to 'create' - would it count as a member? – digitalWestie Apr 13 '11 at 13:46
  • 1
    Yes, create is a member action. – Theo Apr 13 '11 at 20:03
  • 1
    But what about adding a "get" without any block? Is it member or collection route? My test showed its collection but with different id variable name. – lzap Apr 19 '11 at 15:12
  • 6
    Are you sure about create being a member action. There is no ID to pass. – George Shaw Nov 21 '13 at 05:42
  • @GeorgeShaw The Id u're wondering about will be nested in the url u are sending to the create-action. Let's say that u have a resources called 'posts' and u've a member called 'vote' nested inside resources :posts; of course. Then u wanna create something like a vote; u wanna a vote on a certain post. U'll be sending to the create-action of vote something like this: 'posts/:id/vote'; comprende? and there u've yr ID u're wondering about :) I hope this helps...(...) – YoniGeek Feb 20 '14 at 14:41
  • 6
    @YoniGeek. Not correct. See http://guides.rubyonrails.org/routing.html, 2.7 Nested Resources. Create is always a collection action as you are adding to the collection. Note that the named create path is always plural. In your example you are adding a new vote to the collection of votes that belong to a member of posts. The id refers to the member post, not the collection of votes being created.The named path would be posts_votes_url, for example. – George Shaw Feb 21 '14 at 06:10
  • @Theo How to give action to the given member/collection of resource. I didn't find on net. Although I can ask it as separate question but I think one will get the answer from the comment section... so. please. – zeal Aug 24 '14 at 22:02
  • @zeal This is three years late, but the action is given after the verb. In the examples above, the actions would be "preview" and "search". – furman87 Jul 21 '17 at 12:47
232
                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)
Amit Patel
  • 15,021
  • 18
  • 64
  • 103
  • 1
    Should use `search_photos_path` instead of `search_photos_url` so people won't think `_path` and `_url` are a difference between the two. – Joshua Pinter Jul 14 '16 at 03:51
33

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!

tybro0103
  • 46,085
  • 33
  • 141
  • 168
15

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]

Nithin
  • 3,633
  • 3
  • 28
  • 54
Beena Shetty
  • 3,584
  • 2
  • 25
  • 30