Elijah
Elijah

Reputation: 281

strange routing issue

Ruby 1.9.2, Rails 3.1

Here is my routes.rb

resources :ads

I thought this entry was supposed to cover pretty much anything that will be placed under /ads URL and have a corresponding method in controller. The strange thing it works for everything under /ads, but when I try to get to ads/:id/delete I receive an error: No route matches [GET] "/ads/50/delete"

If I add explicit entry

match 'ads/:id/delete' => 'ads#delete'

then everything works

entry from controller

 def delete
      @ad=Ad.find(params[:id])
      @ad.destroy
      redirect_to '/ads'
  end

a) I'm trying to figure put why the entry resources :ads doesn't work for /ads/:id/delete b) any pointers as to a simple way to debug routing issues would be appreciated.

UPD: Output for the rake routes

c:\RailsInstaller\work\mebay>rake routes
    ads GET    /ads(.:format)            {:action=>"index", :controller=>"ads"}
        POST   /ads(.:format)            {:action=>"create", :controller=>"ads"}
 new_ad GET    /ads/new(.:format)        {:action=>"new", :controller=>"ads"}
edit_ad GET    /ads/:id/edit(.:format)   {:action=>"edit", :controller=>"ads"}
     ad GET    /ads/:id(.:format)        {:action=>"show", :controller=>"ads"}
        PUT    /ads/:id(.:format)        {:action=>"update", :controller=>"ads"}
        DELETE /ads/:id(.:format)        {:action=>"destroy", :controller=>"ads"}
               /ads/:id/delete(.:format) {:controller=>"ads", :action=>"delete"}

Upvotes: 1

Views: 78

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

The routes that are generated are "RESTful" -- so while you do get access to create, update, delete, etc. your records, you have to make sure you're using the proper REST verb. In your case, it looks like you're trying to send a GET request to your delete path, which won't work. You need to send a DELETE request instead.

If you post your actual code for the link that's used to delete this we can give you a more specific answer, but generally your delete link should look something like this:

<%= link_to "Delete", ad_path(@ad), :method => :delete %>

This will send a DELETE request to /ads/:id, which Rails will know should call your destroy action.

The official Ruby on Rails Routing guide in @Tilo's answer is a great resource that you should read through before moving forward.

Upvotes: 2

Related Questions