2013-03-08 31 views
0

我正在处理包括向Typo添加功能的任务。无法弄清楚Rails路由帮助程序应该如何查找

rake routes显示:

admin_content /admin/content      {:controller=>"admin/content", :action=>"index"} 
       /admin/content(/:action(/:id))  {:action=>nil, :id=>nil, :controller=>"admin/content"} 

我需要创建以下REST风格的路线匹配的路线帮手:/admin/content/edit/:id和URL的一个例子是/admin/content/edit/1

但我想不出该怎么办它。我尝试了admin_content_path(edit,some_article)之类的东西,但没有奏效。 (some_article只是一个文章对象)

routes.rb文件:

# some other code 

# Admin/XController 
%w{advanced cache categories comments content profiles feedback general pages 
resources sidebar textfilters themes trackbacks users settings tags redirects seo post_types }.each do |i| 
match "/admin/#{i}", :to => "admin/#{i}#index", :format => false 
match "/admin/#{i}(/:action(/:id))", :to => "admin/#{i}", :action => nil, :id => nil, :format => false 
end 

#some other code 

非常感谢您的帮助!

+0

您是否试过'admin_content_path(:action => edit,:id => some_article)'? – depa 2013-03-08 16:58:51

+0

请添加'route.rb'文件的内容 – benchwarmer 2013-03-08 17:00:16

+0

'edit_admin_content_path(文章)'你试过这个吗? – MrYoshiji 2013-03-08 17:22:43

回答

1

如果您正在使用REST风格的路线,为什么不使用Rails的默认路由?

所以你routes.rb看起来像

namespace :admin do 
    resources :content 
    resources :advanced 
    resources :categories 
    resources :comments 
    ... 
    <etc> 
end 

这并承担所有的控制器都在文件夹admin中(但您的评论,这似乎是这样。

如果你这样做,你可以使用标准路线帮手:edit_admin_content_path

如果你想手动做,你应该尝试给你的路线添加一个名字,例如:

match "/admin/#{i}/:action(/:id)" => "admin/#{i}", :as => "admin_#{i}_with_action" 

,然后你应该这样做

admin_content_with_action(:action => 'edit', :id => whatevvvva) 

作为一个侧面说明:我真的不喜欢的元编程在config/routes.rb,如果不管你真的发现默认的资源没有合适人选,我会建议使用方法,而不是在(如解释here

因此,例如,您config/routes.rb你可以这样写:

def add_my_resource(resource_name) 
    match "/#{resource_name}", :to => "#{resource_name}#index", :format => false 
    match "/#{resource_name}(/:action(/:id))", :to => "#{resource_name}", :as => 'admin_#{resource_name}_with_action", :action => nil, :id => nil, :format => false 
end 

namespace :admin do 
    add_my_resource :content 
    add_my_resource :advanced 
    add_my_resource :categories 
    ... 
end 

其中IMHO是更具有可读性。

但我的建议,除非你真的需要避免它,否则将使用标准resources,因为你似乎没有添加任何特别的东西。

HTH。

+0

非常感谢您的帮助!使用Typo(开源RoR博客)处理我的任务时遇到了此问题。 – 2013-03-19 09:35:28