如果你只记住两个关键词,Rails 的大方向就不会错:
Convention over ConfigurationDRY
把它翻成工务署语言就是:
- 先约定统一施工标准,减少每栋楼都重新报批
- 同一份规则别在十个地方抄十遍
比如这段控制器:
class PermitsController < ApplicationController
def index
@permits = Permit.order(created_at: :desc)
end
end如果你有这条路由:
resources :permits又有这个模板文件:
app/views/permits/index.html.erb那 Rails 很多时候不需要你再手写“去渲染哪个模板”。
它会按工务署标准自己找到:
PermitsController#index
-> app/views/permits/index.html.erb
-> 外层再套 app/views/layouts/application.html.erb这就是“约定优于配置”。
不是魔法。
是命名、目录、职责边界都被工务署标准化了。