Routes - Making Delete a Collection Operation

Hi,

Using “resources :object” in my routes file, the DELETE operation
operates by default on an individual member (i.e. it requires an ‘id’ to
be specified).

I’d like to change this so that DELETE is a collection route, so that I
can call it without having to specify an id and still have the route
match through to the destroy method.

I’m new to Rails routes, so any help would be much appreciated!

Thanks,

  • Nex

Hi,

a collection routes is meant to work on the whole collection. Example
for a
collection delete would be for example to delete all books that are out
of
print. If you want to delete a particular book the member route is
perfect…how would you otherwise know in your controller, which book
to
delete?

Markus


http://www.communityguides.eu

On 20 March 2011 17:12, Peter L. [email protected] wrote:


DELETE /appearances

How will this identify which collection of appearances you want to
delete?

Colin

That’s a good question, and perhaps I’m heading down the wrong path. But
I intend to pass a parameter in the HTTP body that will determine the
type of appearances objects to remove. Because I would like the delete
operation to delete a collection of appearances dependent on whether
they belong to a certain person for example.

So my intention was to send the DELETE, and then a ‘person’ parameter in
the body of the DELETE request would determine which appearances to
delete.

This widens the discussion slightly, but perhaps you could give guidance
on the validity of this model of doing things?

Many thanks again

Colin L. wrote in post #988435:

On 20 March 2011 17:12, Peter L. [email protected] wrote:


DELETE /appearances

How will this identify which collection of appearances you want to
delete?

Colin

Well, you can prevent Rails from creating that route for you using
:except
or :only in the routes.rb and then add a collection route with the same
name
(if you need examples on that, you can follow the link in my signature).
Technically that solves your problem. But this is for removing a
collection,
for example an admin action to delete all logs older than x-month. Can
you
explain with an example what your really want to do?

Markus


http://www.communityguides.eu

Hi Markus,

I think I haven’t been clear enough, the default resource is set up for
individual members, rake routes gives me this:


DELETE /appearances/:id(.:format)

Whereas I actually want to create a route for deleting a collection:


DELETE /appearances

But I don’t believe this is created by default, I get a no route error
when calling delete on just the controller without an id.

So in short - I actually want to have a route to delete a whole
collection, but the default resources method in routes.rb creates a
delete method for a member, and doesn’t set up a route for deleting a
collection. I would like to know how to add a delete route for a
collection.

Thanks!

Markus P. wrote in post #988427:

Hi,

a collection routes is meant to work on the whole collection. Example
for a
collection delete would be for example to delete all books that are out
of
print. If you want to delete a particular book the member route is
perfect…how would you otherwise know in your controller, which book
to
delete?

Markus


http://www.communityguides.eu

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