Phoenix Rising
2009-Oct-20 00:12 UTC
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.
Robert Walker
2009-Oct-20 15:35 UTC
Re: RESTful Routing - Difference between new, collection, me
Phoenix Rising 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. -- Posted via http://www.ruby-forum.com/.