Forum: Ruby on Rails Restful delete/destroy

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
elle (Guest)
on 2009-03-25 05:31
(Received via mailing list)
Hello,

I am sure this is a simple problem but for some reason I can't get it
to work.
I am trying to delete a record. My routes.rb has:
map.resources :ads

...the AdsController has:
  def destroy
    @ad = Ad.find(params[:id])
    @ad.destroy

    respond_to do |format|
        format.html { redirect_to(ads_url) }
        format.xml  { head :ok }
    end
  end

...and the index.html.erb has:
<ul>
<% @ads.each do |ad| %>
  <li><%= link_to h(ad.name), ad %> [<%= link_to "Delete", ads_path
(ad), :method => :destroy %>]</li>
<% end %>
</ul>

But instead of destroying a record, it creates a new empty record.
Also, when looking at the url of 'Delete' I get
http://localhost:3000/ads.%23%3Cad:0x24681b8%3E


TIA,
Elle
Andrew B. (Guest)
on 2009-03-25 07:10
(Received via mailing list)
try changing the delete link to ad_path(ad) instead of ads_path(ad).
elle (Guest)
on 2009-03-25 07:28
(Received via mailing list)
I get:

ActionController::MethodNotAllowed
Only get, put, and delete requests are allowed.

which I don't understand -- since isn't the request delete??
elle (Guest)
on 2009-03-25 10:06
(Received via mailing list)
Solved.
1. As Andrew suggested, I used ad_path(ad) in singular

2. I left the name of the method as destroy, so:
def destroy
...
end

3. Changed the :method => :delete
so, my delete link ended up as:
<%= link_to 'Delete', ad_path(ad), :method => :delete %>


Thanks,
Elle
This topic is locked and can not be replied to.