ChatGPT解决这个技术问题 Extra ChatGPT

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.


T
Theo

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.


What about the create action? Say I wanted to make an alternative to 'create' - would it count as a member?
Yes, create is a member action.
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.
Are you sure about create being a member action. There is no ID to pass.
@YoniGeek. Not correct. See 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.
A
Amit Patel
                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)

Should use search_photos_path instead of search_photos_url so people won't think _path and _url are a difference between the two.
t
tybro0103

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!


N
Nithin

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]


nice example. explanation at the top could have been better though.