Ruby-on-rails Rails 中的单数或复数控制器和助手名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/646951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:03:31  来源:igfitidea点击:

Singular or plural controller and helper names in Rails

ruby-on-railsnaming-conventions

提问by allyourcode

Is there any disadvantage to using singular names for controllers and helpers? Nothing seems to rely on this. It even seems helpers don't have to make the same choice about singular vs. plural as their corresponding controllers, at least according to my limited experimentation. Is that true?

对控制器和助手使用单数名称有什么缺点吗?似乎没有什么依赖于此。至少根据我有限的实验,助手似乎不必像他们相应的控制器一样对单数和复数做出相同的选择。真的吗?

回答by jpgeek

Definitely plural.

肯定是复数

With restful routing and a singular controller

使用宁静的路由和单一的控制器

Controller:

控制器:

dog_controller.rb  

Routes:

路线:

map.resources :dogs  # => blows up  
map.resources :dog  # is ok, but...  
dogs_path # => blows up  
dog_path  # => ok  

Using a plural controller

使用复数控制器

Controller:

控制器:

dogs_controller.rb

Routes:

路线:

map.resources :dogs  
dogs_path # => ok  
dog_path # => ok  

rails generate controller --helphas plural examples:

rails generate controller --help有复数例子:

Example:
`rails generate controller CreditCards open debit credit close`

CreditCards controller with URLs like /credit_cards/debit.
    Controller: app/controllers/credit_cards_controller.rb
    Test:       test/controllers/credit_cards_controller_test.rb
    Views:      app/views/credit_cards/debit.html.erb [...]
    Helper:     app/helpers/credit_cards_helper.rb

回答by Can Berk Güder

Using plural names for controllers is just a convention.

对控制器使用复数名称只是一种约定。

Plural names usually sound more natural (especially for controllers that are tied directly to a specific model: User -> Users, etc.), but you can use whatever you want.

复数名称通常听起来更自然(特别是对于直接绑定到特定模型的控制器:User -> Users 等),但您可以使用任何您想要的名称。

As for helpers, all helpers are available for all controllers by default, so technically, how you name your helpers doesn't matter at all. It's just another convention to keep a controller's helper functions in a helper with the same name as the controller.

至于助手,默认情况下所有助手都可用于所有控制器,因此从技术上讲,您如何命名助手根本无关紧要。将控制器的辅助函数保存在与控制器同名的辅助函数中只是另一种约定。

回答by Ryan

A Model is singular because it references a single object like User. A controller is plural because it is the controls (methods) for the collection of Users. How one names the routes is all up to that individual developer. I've never had a user complain that a URL for a web request is singular or plural. The end result to maintain a common convention for current and future contributors while serving quality page displays or the API requests for the end users.

模型是单数的,因为它引用单个对象,如用户。控制器是复数,因为它是用户集合的控件(方法)。如何命名路由完全取决于个人开发人员。我从来没有遇到过用户抱怨 Web 请求的 URL 是单数还是复数。最终结果是在为最终用户提供高质量页面显示或 API 请求的同时,为当前和未来的贡献者维护通用约定。

回答by Nerian

You have a very complete explanation in the Rails guides: http://edgeguides.rubyonrails.org/routing.html#resource-routing-the-rails-default

您在 Rails 指南中有非常完整的解释:http: //edgeguides.rubyonrails.org/routing.html#resource-routing-the-rails-default

回答by Teemu Leisti

It is the Rails convention that one controller handles one model, whether one or more instances of that model can exist during runtime. However, you can have a Rails application where (some of) the controllers (and the associated views) are not associated with any particular model, but rather handle a more complex set of functionality. In this case, the automatic pluralization doesn't make any sense.

一个控制器处理一个模型是 Rails 的惯例,无论该模型的一个或多个实例可以在运行时存在。但是,您可以拥有一个 Rails 应用程序,其中(某些)控制器(和相关视图)不与任何特定模型相关联,而是处理更复杂的功能集。在这种情况下,自动复数没有任何意义。

The Rails application I'm currently working on fits into this category, and it's simply an irritation to me that Rails expects that the identifiers I define as a singular in one place are then used in their plural forms in other places. For example, I might want to define something like this in config/routes.rb:

我目前正在开发的 Rails 应用程序就属于这一类,Rails 期望我在一个地方定义为单数的标识符然后在其他地方以复数形式使用,这让我很恼火。例如,我可能想在以下内容中定义类似的内容config/routes.rb

  resource :dashboard, :only => [:show]

and then I want a controller DashboardControllerto display summary information about certain aspects of the application, gathering information from more than one database table. So here, Dashboarddoes not refer to any model of the application, and it would be just weird to have the controller's name be DashboardsController.

然后我想要一个控制器DashboardController显示有关应用程序某些方面的摘要信息,从多个数据库表中收集信息。所以在这里,Dashboard不指应用程序的任何模型,控制器的名称是DashboardsController.

I found a good solution to the irritation of automatic pluralization in this answer. In short, edit file config/initializers/inflections.rband add the words you don't want to be automatically pluralized to this definition:

我在这个答案中找到了一个很好的解决自动复数的刺激的方法。简而言之,编辑文件config/initializers/inflections.rb并将您不想自动复数的单词添加到此定义中:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable %w( dashboard foo bar baz )
end

回答by Teemu Leisti

If the controller is a resource then it must be plural...

如果控制器是一个资源,那么它必须是复数...

For example

例如

Controller

控制器

articles_controller.rb

Model

模型

article.rb

But you can use singular controller names when you do not have corresponding models like

但是当您没有相应的模型时,您可以使用单数控制器名称,例如

welcome_controller.rb

回答by Mukesh Singh Rathaur

The naming convention of controllers in Rails favors pluralizationof the last word in the controller's name, although it is not strictly required(e.g. ApplicationController).

Rails中控制器的命名约定支持控制器名称中最后一个单词的复数形式,尽管这不是严格要求的(例如ApplicationController)。

For example, ClientsControlleris preferable to ClientController, SiteAdminsControlleris preferable to SiteAdminController or SitesAdminsController, and so on.

例如,ClientsController优先于ClientControllerSiteAdminsController优先于SiteAdminController 或SitesAdminsController,等等。

Following this convention will allow you to use the default route generators (e.g. resources, etc) without needing to qualify each :pathor :controller, and will keep URL and path helpers' usage consistent throughout your application.

遵循此约定将允许您使用默认路由生成器(例如资源等)而无需限定每个:path:controller,并将在整个应用程序中保持 URL 和路径助手的使用一致。

Ref: Controller Naming Convention-Rails Doc

参考:控制器命名约定-Rails 文档

回答by Dillone Hailei Wang

I feel better when I use singular for Controller name

当我使用单数作为控制器名称时我感觉更好

回答by nitecoder

Using plurals just sounds better, and then if you have a controller that handles a singular resourse, ie user, then you can still name the url /user.

使用复数听起来更好,然后如果您有一个处理单数资源的控制器,即用户,那么您仍然可以将 url 命名为 /user。

With helpers there is often no need to have a helper for every controller, and often there will be helper methods you can use ascorss multiple controllers and rather litter them all through your application helper you could put them in custom helpers instead like eg layout_helper or any other well named file.

有了助手,通常不需要为每个控制器都有一个助手,而且通常会有助手方法,您可以使用多个控制器,而是将它们全部放在应用程序助手中,您可以将它们放在自定义助手中,例如 layout_helper 或任何其他命名良好的文件。