Rails控制器命名规则疑问:为何普通控制器名称采用复数形式而application_controller为单数形式
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_actioncallbacks (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

