RESTful Routing - Difference between new, collection, member

I’m re-working an application from scratch to make use of REST-based
resource routing for the first time. In doing some reading, I found
the Rails guide (http://guides.rubyonrails.org/routing.html) to be
really helpful, but I don’t quite understand the difference between
“new”, “collection” and “member” when defining additional routes on a
resource. Could somebody clear that up a little - preferably with
some “real world” examples? The examples in the rails guide seemed
pretty ambiguous.

Phoenix R. wrote:

I’m re-working an application from scratch to make use of REST-based
resource routing for the first time. In doing some reading, I found
the Rails guide (http://guides.rubyonrails.org/routing.html) to be
really helpful, but I don’t quite understand the difference between
“new”, “collection” and “member” when defining additional routes on a
resource. Could somebody clear that up a little - preferably with
some “real world” examples? The examples in the rails guide seemed
pretty ambiguous.

An action that acts upon a collection of the resource is defined with
collection and has a route that reflects that:

For example the index action acts upon a collection of the resource:
GET: /posts

An action that acts upon a single instance of a resource is defined with
member and has a route that reflects that:

The show, create, update and delete actions are examples:
GET: /posts/1
POST: /posts/1
PUT: /posts/1
DELETE: /posts/1

An action using ‘new’ also acts upon the collection but allows for
additional actions that create new instances.

Example:
map.resources :photos, :new => { :upload => :post }

Allows Rails to recognize the following request:
POST: /photos/upload

and route the request to the upload action of the resource’s controller.
This would be in addition to the ‘normal’ CRUD request:
POST: /photos

This request could also be used to create new instances of the photos
resource by routing to the create action.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs