How to delete all records in a table using a link

Are you sure that is a good idea? What will happen when google
follows the link? It might be better as a post rather then a get.

Colin

Colin Law wrote:

<%= link_to 'Destroy All', @rushing_offenses.all.destroy!, :confirm =>
format.xml { head :ok }
end
end

I simply want to place a link on one of my pages that when clicked, it
destroys all of the data inside the table (but not the table or model
itself) and then redirects back to the main page which is
@rushing_offenses.

Are you sure that is a good idea? What will happen when google
follows the link? It might be better as a post rather then a get.

Colin

Hi Colin,

I'm just going to be using it for my development platform. I simply
want a link so that I can quickly remove all data in a particular table
and then be able to re-populate it with some new data..

Can you provide me with a way to do that?

Actually I was talking rubbish, the :method => :delete will make it a post.
I think you want something like
<%= link_to 'Destroy All', :action => 'destroyall', :confirm => 'Are
you sure?', :method => :delete %>
but you might need something in routes as well, I am not sure.

Colin

Try this:

View:

<%= link_to ‘Destroy All’, :action => ‘destroyall’, :confirm
=> ‘Are you sure?’, :method => :delete %>

Routes:

map.resources rushing_offenses, :collection => { :destroyall =>
:delete }

Controller:

    def destroyall
RushingOffense.destroy_all
# or RushingOffense.delete_all (very speedy: delete from rushing_offenses)
respond_to do format.html { redirect_to(rushing_offenses_url) }
format.xml { head :ok }
end
end

I think you want

  map.resources :rushing_offenses, :collection => {:delete => :destroyall}

Rick Denatale wrote:

I think you want

map.resources :rushing_offenses, :collection => {:delete =>
:destroyall}

Thanks Rick.

When I do this I get the following error:

Invalid HTTP method specified in route conditions: {:method=>
:destroyall} (Argument Error)

Oops, that was a brain fart on my part, yes the order should be
:destroyall => :delete

My index.html.erb file under views\rushing_offenses looks like:

<h1>Rushing Offense</h1>
<h5>Current Time is <%= Time.now.to_s(:long) %> </h5>

...

<%= link_to 'Destroy All', :action => 'destroyall', :confirm => 'Are you
sure?', :method => :delete %>

Maybe

  <%= link_to "Destroy All", rushing_offenses_path(:method =>
:delete), :confirm> "Are you sure?" %>

From your earlier post, it looks like you were ending up with a url of
rushing_offense/destroyall?method=delete&confirm=Are+you+sure%3F and a
get request which ends up getting routed to the show action.

Okay, going to simplify this a bit..

Currently, I commented out the maps because with them in they don't seem
to be working (whether I use one or the other)

# map.resources :rushing_offenses, :collection => { :destroyall =>
:delete }
# map.resources :rushing_offenses, :collection => {:delete =>
:destroyall}

My link is set to:

<%= link_to "Destroy All", rushing_offenses_path(:method =>
:destroyall), :confirm => "Are you sure?" %>

3 things:
- that needs to be link_to 'blah', some_path, :method => :delete
- you can't makeup methods here
- the resources stuff should be :destroyall => :delete

Fred

Routes.rb:

  map.resources :people, :collection => {:scrub => :delete}

Index.html.erb:

<%= link_to 'Kill em all', scrub_people_path, :confirm => 'Are you
sure?', :method => :delete %>

people_controller.rb:

  def scrub
    Person.destroy_all
    redirect_to people_url
  end

works for me...

Okay, I believe I understand what you are saying (I'm just probably
missing something small now)

Here's the updated...

Routes:

map.resources :rushing_offenses
map.resources :rushing_offenses, :collection => { :destroyall => :delete
}

Link:

<%= link_to "Destroy All", rushing_offenses_path, :confirm => "Are you
sure?", :method => :delete %>

Controller Method:

def destroyall
    RushingOffense.destroy_all
    redirect_to rushing_offenses_url
end

Just one entry in routes.rb for rushing_offenses

map.resources :rushing_offenses, :collection => {:destroyall => :delete}

(the :collection or :member are additions to the standard resource
mappings, you don't need to specify a second line)

and tweak the link definition

<%= link_to "Destroy All", destroyall_rushing_offenses_path, :confirm =>
"Are you
sure?", :method => :delete %>

That should do it... For fun, run a

rake routes >routes.lst

to get a file where you can view what all your routes look like.

And be sure to restart your app if you change routes.rb

Ar Chron wrote:

Just one entry in routes.rb for rushing_offenses

map.resources :rushing_offenses, :collection => {:destroyall => :delete}

(the :collection or :member are additions to the standard resource
mappings, you don't need to specify a second line)

and tweak the link definition

<%= link_to "Destroy All", destroyall_rushing_offenses_path, :confirm =>
"Are you
sure?", :method => :delete %>

That should do it... For fun, run a

rake routes >routes.lst

to get a file where you can view what all your routes look like.

PERFECT! It works now.

So, just to update..

I had two routes for map.resources (that was probably problem number 1)
The link had to be exactly as you specified above (that was problem
number 2)
Restarted my server and everything is working as intended.

So, now that I have this issue fixed, let me make sure I really
understand what I just did (or else I'm not going to learn much here)

Map Routes:

When I'm supplying a :collection symbol it really means that it's just
adding to the map.resources :rushing_offenses correct? And the end
portion of the map line is stating that whenever I call the method
:delete, it's going to use the controller method for :destroyall?

If I'm reading this right, when would I add another map route for
rushing_offenses or would I just extend that map route by adding other
hash/blocks?

Links:

The first segment is just the name of the link as it appears on the
page.

The second segment is the path to where my custom method exists? I'm
not sure why the naming of the path was destroyall_rushing_offenses_path
unless it is structured so that it matches method_controller_path (if so
then I understand it perfectly).

The third segment was the name of the method we mapped to?

Let me know if I reach a 90% on this tutorial.

thanks everyone - I'm learning a lot (even from this small piece)

Älphä Blüë wrote:

When I'm supplying a :collection symbol it really means that it's just
adding to the map.resources :rushing_offenses correct?

Yes, an action that works on a collection of the resource.

And the end
portion of the map line is stating that whenever I call the method
:delete, it's going to use the controller method for :destroyall?

Nope...

map.resources :rushing_offenses, :collection => {:destroyall => :delete}

defines an additional route that works on the collection of
rushing_offenses.
Your action is destroyall, which is called as an HTTP delete method

If I'm reading this right, when would I add another map route for
rushing_offenses or would I just extend that map route by adding other
hash/blocks?

Yup...

http://api.rubyonrails.org/classes/ActionController/Resources.html

you can add additional actions for the collection, and/or additional
actions to operate on individual rushing_offense instances

Links:

The first segment is just the name of the link as it appears on the
page.

The second segment is the path to where my custom method exists? I'm
not sure why the naming of the path was destroyall_rushing_offenses_path
unless it is structured so that it matches method_controller_path (if so
then I understand it perfectly).

The third segment was the name of the method we mapped to?

Let me know if I reach a 90% on this tutorial.

First segment is the text to render as the link.

Second segment is the 'controller, action' specification. If you're
using the map.resources, then Rails creates handy aliases, like
   destroyall_rushing_offenses_path
The old school approach was
<%= link_to 'Destroy All', :controller => 'rushing_offenses', :action =>
'destroyall', :method => :delete %>

Third segment is telling Rails what method you want for the browser
request (can be :get, :put, :post, :delete, or :any)

Follow that link above for more/better info about resource-based
routing.