简体   繁体   English

如何命名嵌套的控制器和路由?

[英]How to name nested controllers and routes?

I'm working on creating a wiki app from scratch with the following organization: 我正在与以下组织从头开始创建Wiki应用程序:

Main Controller: Wiki Nested Controller: WikiCategories Nested Controller: WikiArticles 主控制器:Wiki嵌套控制器:WikiCategories嵌套控制器:WikiArticles

In my config/routes.rb I have: 在我的config / routes.rb中,我有:

resource :wiki do
  resources :wiki_categories, :as => :categories
  resources :wiki_articles, :as => :articles
end

I've chosen to name the categories and articles controllers as WikiCategories and WikiArticles to differentiate from other category and article controllers that I want to make in the future under a blog nesting. 我选择将类别和文章控制器命名为WikiCategories和WikiArticles,以与将来希望在博客嵌套下创建的其他类别和文章控制器区分开。

This gives me the following routes: 这给了我以下路线:

/wiki/wiki_categories/new
/wiki/wiki_articles/new

Is there any way to rewrite the routes to be: 有什么办法可以改写路由:

/wiki/categories/new
/wiki/articles/new

... while still using the WikiCategories and WikiArticles controller names? ...仍在使用WikiCategories和WikiArticles控制器名称?

Yes it is by specifying the controller , the resource can then be named whichever way you like. 是的,它是通过指定controller ,然后可以使用您喜欢的任何方式来命名资源。

resource :wiki do
  resources :categories, controller: 'wiki_categories'
  resources :articles, controller: 'wiki_articles'
end

Please see the guide for further information. 请参阅指南以获取更多信息。

You can use the path: option as follows: 您可以使用path:选项,如下所示:

resource :wiki do
  resources :wiki_categories, path: 'categories', :as => :categories
  resources :wiki_articles, path: 'articles', :as => :articles
end

Which will give you: 这会给你:

/wiki/categories/...
/wiki/articles/...

See Translated Paths section of the guides for further details. 有关更多详细信息,请参见指南的“ 翻译路径”部分。

I've chosen to name the categories and articles controllers as WikiCategories and WikiArticles to differentiate from other category and article controllers that I want to make in the future under a blog nesting. 我选择将类别和文章控制器命名为WikiCategories和WikiArticles,以与将来希望在博客嵌套下创建的其他类别和文章控制器区分开。

IMO, it seems like you're bucking convention a bit. IMO,看来您有点违反常规。 As discussed in Controller Namespaces and Routing , why not do: 控制器名称空间和路由中所述 ,为什么不这样做:

namespace :wiki do
  resources :categories, :articles
end

Which will give you: 这会给你:

    wiki_categories GET    /wiki/categories(.:format)              wiki/categories#index
                    POST   /wiki/categories(.:format)              wiki/categories#create
  new_wiki_category GET    /wiki/categories/new(.:format)          wiki/categories#new
 edit_wiki_category GET    /wiki/categories/:id/edit(.:format)     wiki/categories#edit
      wiki_category GET    /wiki/categories/:id(.:format)          wiki/categories#show
                    PATCH  /wiki/categories/:id(.:format)          wiki/categories#update
                    PUT    /wiki/categories/:id(.:format)          wiki/categories#update
                    DELETE /wiki/categories/:id(.:format)          wiki/categories#destroy
      wiki_articles GET    /wiki/articles(.:format)                wiki/articles#index
                    POST   /wiki/articles(.:format)                wiki/articles#create
   new_wiki_article GET    /wiki/articles/new(.:format)            wiki/articles#new
  edit_wiki_article GET    /wiki/articles/:id/edit(.:format)       wiki/articles#edit
       wiki_article GET    /wiki/articles/:id(.:format)            wiki/articles#show
                    PATCH  /wiki/articles/:id(.:format)            wiki/articles#update
                    PUT    /wiki/articles/:id(.:format)            wiki/articles#update
                    DELETE /wiki/articles/:id(.:format)            wiki/articles#destroy

Then, create namespaced controllers, something like: 然后,创建命名空间控制器,如下所示:

app/controllers/wiki/categories.rb

class Wiki::CategoriesController < ApplicationController
  ...
end

and

app/controllers/wiki/articles.rb

class Wiki::ArticlesController < ApplicationController
  ...
end

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

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