What do convention over configuration and DRY mean in Rails, and which configuration does a Rails CRUD for people never write because of them?
answer
- the Rails way as default
- no table or column mapping
- implicit render of the matching template
- form_with infers URL, method, param key
- one authoritative place per fact
basics
~20 sIn Rails, convention over configuration means names and locations imply behaviour, and DRY means each fact lives in one place; a people CRUD declares no table mapping, columns, routes per action, template paths or form URLs, because Rails derives them.
solid answer
~50 sConvention over configuration means Rails assumes sensible defaults (names, file locations, columns) and only asks you to configure the exceptions. DRY, "every piece of knowledge must have a single, unambiguous, authoritative representation", means each fact is stated once. For a people CRUD that removes a lot: `Person` maps to `people` and reads its attributes from the table's columns, so nothing is declared twice; `created_at` and `updated_at` are filled automatically; `resources :people` defines the routes for the seven standard actions; an action that does not call `render` renders `app/views/people/<action>.html.erb`; and `form_with model: @person` infers the URL, the HTTP method and the `person` param key. Shared markup goes into partials, shared controller setup into `before_action`. The cost is that conventions must be learned, and code that fights them, such as hand-named controllers or legacy tables, needs explicit configuration.
code
ruby · 14 lines# config/routes.rb
resources :people
# app/controllers/people_controller.rb
class PeopleController < ApplicationController
def index
@people = Person.order(:name)
# no render call: Rails renders app/views/people/index.html.erb
end
def show
@person = Person.find(params[:id])
end
endgo deeper
Define both principles in a sentence each and give concrete examples: table mapping, implicit render, resources routes, form_with inference.
Explain the mechanisms behind the examples and where configuration takes over: legacy tables, non-standard controllers, words the inflector gets wrong.
Weigh the trade: shared knowledge and less code against surprise for newcomers, and recognise when DRY abstractions couple things that should change separately.
Decide where a codebase may deviate from conventions and how those deviations are documented, so the shared knowledge stays reliable.
## The two principles The Rails guides name two guiding principles: - **Don't Repeat Yourself (DRY)**: "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." - **Convention over configuration**: Rails "has opinions about the best way to do many things in a web application, and defaults to this set of conventions, rather than require that you define them yourself through endless configuration files." They reinforce each other. A convention is a fact the whole team, and every Rails component, already knows, so nobody has to repeat it in configuration. ## What a people CRUD does not write | Concern | What Rails derives | What you would write without the convention | |---|---|---| | Table mapping | `Person` uses the `people` table | a table name per model | | Attributes | read from the table's columns at runtime | a field list duplicating the schema | | Timestamps | `created_at`, `updated_at` set on create and update | callbacks setting them | | Routes | `resources :people` gives index, show, new, create, edit, update, destroy | a route declaration per action | | Templates | an action without `render` renders `people/<action>` | a template path per action | | Forms | `form_with model: @person` picks URL, POST or PATCH, and `person[...]` field names | the URL, method and field prefix by hand | | Foreign keys | `belongs_to :person` uses `person_id` | a key name per association | Each row is a decision taken once, for every Rails app, so it is never discussed per project. ## DRY in the same app - **The schema is the source of attributes**: Active Record models do not list their columns; adding a column in a migration makes the attribute appear. - **Validations once, used everywhere**: a `validates :name, presence: true` on the model is what every form, API call and console session goes through, and its messages feed the form's error display. - **Partials** such as `people/_form.html.erb` are shared by `new` and `edit`. - **`before_action :set_person`** loads the record once for `show`, `edit`, `update` and `destroy`. ## Where it stops being free 1. **The conventions must be learned.** A newcomer sees `render` missing from an action and wonders where the HTML comes from. Interviews probe exactly this. 2. **Exceptions need configuration.** A legacy table called `tbl_person`, a controller deliberately named differently, or a word the inflector gets wrong all require explicit settings, and those settings are the places future readers will be surprised. 3. **DRY can be overdone.** Extracting two similar-looking pieces of code into one shared abstraction couples things that may change for different reasons. DRY is about one source of truth for a *fact*, not about removing every repeated line. ## The comparison interviewers sometimes ask for In frameworks that map objects to tables and URLs to handlers through explicit declarations, such as annotations on every entity and route, the same information exists, written out for each class. That is easier to read in isolation and harder to keep consistent. Rails chooses the opposite trade: less code, more shared knowledge. Neither is free; what matters in an interview is naming what each side pays. ## How to talk about it A strong answer gives one concrete example per principle, such as the implicit render and the schema-derived attributes, and one example of when the convention is overridden, which shows you know its edges.
- What does a Rails controller action render when it never calls `render` or `redirect_to`?It renders the template matching the controller and action, such as `app/views/people/index.html.erb` for `PeopleController#index`, inside the controller's layout. If no template exists, a browser request raises a missing-template error, while other request formats get an empty `204 No Content` response.
- Give an example where applying DRY too eagerly hurts a Rails app.Sharing one partial between an admin and a public profile page because they look alike today. When one page needs a field the other must hide, the partial grows flags and conditionals. Two templates that repeat some markup but change independently are easier to maintain than one that serves two masters.
- Convention over configuration means Rails apps cannot be configured.
- Active Record models must list their columns as attributes.
- Every controller action has to call render explicitly.
- DRY means no two lines of code may ever look alike.
- form_with always needs an explicit url: and method: option.