Restful delete/destroy


#1

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:

    <% @ads.each do |ad| %>
  • <%= link_to h(ad.name), ad %> [<%= link_to "Delete", ads_path (ad), :method => :destroy %>]
  • <% end %>

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<ad:0x24681b8>

TIA,
Elle


#2

try changing the delete link to ad_path(ad) instead of ads_path(ad).


#3

I get:

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

which I don’t understand – since isn’t the request delete??


#4

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