Rails 中的单数或复数控制器和助手名称

发布于 2024-07-15 06:22:05 字数 90 浏览 5 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

笑忘罢 2024-07-22 06:22:05

绝对是复数

使用 Restful 路由和单数控制器

Controller:

dog_controller.rb  

Routes:

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

使用复数控制器

Controller:

dogs_controller.rb

Routes:

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

railsgeneratecontroller --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

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 --help has plural examples:

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
染年凉城似染瑾 2024-07-22 06:22:05

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

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

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

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.

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.

我还不会笑 2024-07-22 06:22:05

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

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.

夏末的微笑 2024-07-22 06:22:05

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

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

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

然后我想要一个控制器 DashboardController 来显示有关应用程序某些方面的摘要信息,从多个数据库表收集信息。 因此,在这里,Dashboard 并不引用应用程序的任何模型,并且控制器的名称为 DashboardsController 会很奇怪。

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

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

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.

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:

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

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

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

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable %w( dashboard foo bar baz )
end
櫻之舞 2024-07-22 06:22:05

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

例如

控制器

articles_controller.rb

模型

article.rb

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

welcome_controller.rb

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
九歌凝 2024-07-22 06:22:05

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

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

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

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

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

For example, ClientsController is preferable to ClientController, SiteAdminsController is preferable to SiteAdminController or SitesAdminsController, and so on.

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

Ref: Controller Naming Convention-Rails Doc

半边脸i 2024-07-22 06:22:05

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

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

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.

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.

圈圈圆圆圈圈 2024-07-22 06:22:05

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

I feel better when I use singular for Controller name

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文