views:

101

answers:

1

Hi all, I have a page model and a pages_controller within an admin namespace. My routes file looks like this:

  map.resources :pages, :only => [:index,:show]

  map.resources :admin, :only => [:index]

  map.namespace :admin do |admin|
    admin.resources :pages
  end

I am not able to figure out the correct method to create a link for deleting a page (In the same way the scaffold generator generates a delete link on the index page).

Any ideas on the correct parameters for the link_to function?

TIA, Adam

+2  A: 

rake routes is your friend here. It'll spit out the list of your generated routes - particularly useful if you have a bunch of nested or custom routes.

the paths will be

admin_pages_path #(with GET) routes to :controller => 'admin/pages', :action => 'index'
admin_pages_path #(with POST) routes to :controller => 'admin/pages', :action => 'create'
new_admin_page_path #(with GET) routes to :controller => 'admin/pages', :action => 'new'
edit_admin_page_path(:id) #(with GET) routes to :controller => 'admin/pages', :action => 'edit'
admin_page_path(:id) #(with GET) routes to :controller => 'admin/pages', :action => 'show'
admin_page_path(:id) #(with PUT) routes to :controller => 'admin/pages', :action => 'update'
admin_page_path(:id) #(with DELETE) routes to :controller => 'admin/pages', :action => 'delete'

Your link_to for delete should therefore be:

<%= link_to("delete page", admin_page_path(@page), :confirm => "sure you want to delete this page?", :method => :delete) %>

Note that Rails will work its magic calling to_param on @page, so that you don't have to specify @page.id - useful for an example like this as you often want to use permalinks for 'pages'.

mylescarrick
Thanks Myles,I was misunderstanding the output from rake routes.Adam
Adam