简体   繁体   English

无法弄清楚Rails路由助手的外观

[英]Can't figure out how Rails route helper should look

I am working on an assignment which includes adding a feature to Typo. 我正在从事一项工作,其中包括向Typo添加功能。

rake routes shows: rake routes显示:

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

I need to create a route helper which matches the following RESTful route: /admin/content/edit/:id and an example of url is /admin/content/edit/1 我需要创建一个与以下RESTful路由匹配的路由助手: /admin/content/edit/:id ,URL的示例是/admin/content/edit/1

But I can't figure out how to do it. 但是我不知道该怎么做。 I tried something like admin_content_path(edit,some_article) but it didn't work. 我尝试了类似admin_content_path(edit,some_article)但是它没有用。 (some_article is just an article object) (some_article只是文章对象)

In routes.rb file: 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

Thanks a lot for your help! 非常感谢你的帮助!

If you are using RESTful routes, why not use the Rails default routes? 如果使用的是RESTful路由,为什么不使用Rails默认路由?

So your routes.rb would look like 所以你的routes.rb看起来像

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

This does assume all your controllers are in the folder admin (but from your comment this seems to be the case. 这确实假定您所有的控制器都在admin文件夹中(但是从您的评论看来,情况确实如此。

If you do that, you can just use the standard route-helper: edit_admin_content_path . 如果这样做,则可以只使用标准的路由帮助器: edit_admin_content_path

If you want to do it manually, you should try adding a name to your route. 如果要手动进行操作,则应尝试在路线中添加名称。 Eg as follows: 例如如下:

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

and then you should do something like 然后你应该做类似的事情

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

As a side-note: I really do not like the meta-programming in your config/routes.rb , if for whatever you really find that the default resources are not a right fit, I would advise to use methods instead (as explained here ) 作为一个侧面说明:我真的不喜欢的元编程在你config/routes.rb ,如果不管你真的发现默认的资源是不是合适人选,我会建议使用的方法,而不是(如解释在这里

So for example in your config/routes.rb you would write: 因此,例如,在您的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  

which imho is much more readable. 这是IMHO 可读。

But my advice, unless you really-really need to avoid it, would be to use the standard resources since you do not seem to add anything special. 但是我的建议是,除非您确实需要避免使用它,否则将使用标准resources因为您似乎并没有添加任何特殊内容。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM