You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails控制器命名规则疑问:为何普通控制器名称采用复数形式而application_controller为单数形式

Why Rails Uses Plural for Resource Controllers But Singular for ApplicationController?

Great question—this isn’t just a random quirk, it’s all about Rails’ opinionated conventions that keep code consistent and intuitive. Let’s break it down:

1. Plural Controllers for RESTful Resources

When you run rails generate scaffold Person, you’re creating a controller that manages a collection of resources (all the Person records in your database). Rails follows RESTful design principles, where each controller maps to a set of related actions (index, show, create, update, destroy) that operate on either the entire collection or individual items within it.

Using the plural form (people_controller.rb) makes it immediately clear that this controller is responsible for handling operations related to multiple Person records. Rails’ built-in inflector even handles irregular plurals (like person → people) automatically to keep things consistent.

2. Singular ApplicationController for a Global Singleton

On the other hand, ApplicationController is a single, global base class that all other controllers inherit from. It’s not tied to any specific database resource or collection—instead, it’s where you define cross-cutting concerns that apply to your entire application:

  • Global before_action callbacks (like authentication checks)
  • Shared helper methods
  • Exception handling logic
  • Application-wide configuration

Since there’s only one ApplicationController in your app (it’s a singleton in practice), using the singular form makes sense—it signals that this is a unique, top-level controller rather than one managing a set of resources.

Wrap-Up

In short, Rails uses plural for controllers that manage collections of resources (aligning with REST) and singular for controllers/classes that are single, global entities (like your app’s base controller). This convention helps developers instantly understand a controller’s purpose just by looking at its name.

内容的提问来源于stack exchange,提问作者Hammad Shahid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 10:47:36