我在admin命名空间中有一个页面模型和一个pages_controller。我的路线文件如下所示:
map.resources :pages, :only => [:index,:show]
map.resources :admin, :only => [:index]
map.namespace :admin do |admin|
admin.resources :pages
end
我无法弄清楚创建删除页面的链接的正确方法(与脚手架生成器在索引页面上生成删除链接的方式相同)。
有关link_to函数的正确参数的任何想法吗?
TIA, 亚当
答案 0 :(得分:3)
rake routes
是你的朋友。它会吐出你生成的路由列表 - 如果你有一堆嵌套或自定义路由,这将非常有用。
路径将是
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'
您的link_to for delete应为:
<%= link_to("delete page", admin_page_path(@page), :confirm => "sure you want to delete this page?", :method => :delete) %>
请注意,Rails会在@page上调用to_param
,因此您无需指定@ page.id - 对于此类示例非常有用,因为您经常需要为'pages使用固定链接”。