In a Rails 8.1 app, how do config.generators and lib/templates let a team change what bin/rails generate writes by default?
answer
- config.generators block in application.rb
- orm options: primary_key_type: :uuid
- helper false, system_tests nil
- lib/templates/<base>/<generator>/*.tt
- <%% escapes the generated ERB
basics
~10 sconfig.generators in config/application.rb sets defaults for every generator run, such as UUID keys, no helpers or a different test framework. Files under lib/templates override a built-in generator's templates, for example lib/templates/erb/scaffold/index.html.erb.tt.
solid answer
~40 s`config.generators` takes a block whose calls set default options: `g.orm :active_record, primary_key_type: :uuid` makes migrations write `id: :uuid` and references `type: :uuid`; `g.helper false` stops helper modules; `g.system_tests nil` is what a new app without system tests already has; `g.test_framework :rspec` is set for you by rspec-rails. Each is just the default for a command-line option, so `--helper` still wins on a single run. Templates are the other lever: Rails looks in `lib/templates` before the generator's own `source_root`, at `lib/templates/<base>/<generator>/`, e.g. `erb/scaffold/_form.html.erb.tt`, `rails/scaffold_controller/controller.rb.tt` or `active_record/model/model.rb.tt`. A `.tt` file is ERB rendered by Thor, so the ERB meant for the output is written `<%%`. `config.generators.apply_rubocop_autocorrect_after_generate!` runs RuboCop on the result.
code
ruby · 6 lines# config/application.rb
config.generators do |g|
g.orm :active_record, primary_key_type: :uuid
g.helper false
g.system_tests nil
endgo deeper
Know that config.generators can turn off helpers or change the test framework, and that generated files come from templates you can override.
Explain how a config.generators call becomes a default option, the lib/templates path that mirrors the generator namespace, and the <%% escaping in .tt files.
Use these levers to make generated code match house style, such as UUID keys or authorization in scaffold controllers, and own the upgrade cost of copied templates.
Decide how much standardization belongs in generators versus review, and keep template overrides minimal so upgrades do not freeze old framework idioms in place.
## Two levers over generated code A team that generates code often wants it to come out in **house style**: UUID primary keys, no helper files, RSpec instead of Minitest, a scaffold controller with authorization already in place. Rails offers two levers: | Lever | Changes | Lives in | |---|---|---| | `config.generators` | which sub-generators run and their default options | `config/application.rb` | | `lib/templates` | the file contents a generator renders | `lib/templates/<base>/<generator>/` | ## config.generators `config.generators` yields a `Rails::Configuration::Generators` object. Every call on it becomes a default option, either for all generators or for one namespace: ```ruby # config/application.rb config.generators do |g| g.orm :active_record, primary_key_type: :uuid g.helper false g.system_tests nil g.skip_routes true end ``` - **`g.orm :active_record, primary_key_type: :uuid`**: the model generator's migration gets `create_table :kits, id: :uuid`, and `references` columns get `type: :uuid` so foreign keys match. - **`g.helper false`**: controller, resource and scaffold stop writing `app/helpers/*_helper.rb`. - **`g.system_tests nil`**: no system tests; a new app generated without system tests already has `config.generators.system_tests = nil` in `application.rb`. - **`g.test_framework :rspec`**: switch test generation. Installing **rspec-rails** sets this itself through its railtie. - **`g.template_engine`**, `g.resource_route`, `g.scaffold_controller` and `g.fallbacks` cover the other hooks. These are **defaults, not rules**: `bin/rails generate scaffold Kit --helper` still writes a helper on that one run. `bin/rails generate scaffold --help` shows each option with its current default. The generated `config/environments/development.rb` also carries a commented-out `config.generators.apply_rubocop_autocorrect_after_generate!`, which registers an `after_generate` callback that runs `bin/rubocop -A` on the Ruby files a generator just wrote. ## lib/templates When a generator renders a template, Rails searches the application's **`lib/templates`** first and the generator's own `source_root` second. The path mirrors the generator's namespace: 1. `Erb::Generators::ScaffoldGenerator` looks in `lib/templates/erb/scaffold/` (`index.html.erb.tt`, `_form.html.erb.tt`, `show.html.erb.tt`, ...). 2. `Rails::Generators::ScaffoldControllerGenerator` looks in `lib/templates/rails/scaffold_controller/` (`controller.rb.tt`, `api_controller.rb.tt`). 3. `ActiveRecord::Generators::ModelGenerator` looks in `lib/templates/active_record/model/` (`model.rb.tt`) and `lib/templates/active_record/migration/`. 4. The Minitest generators look under `lib/templates/test_unit/...`. You only add the files you change; any template not overridden still comes from Rails. Copy the original from the Rails source as a starting point, so variables such as `singular_table_name` and `attributes` keep working. ## Writing a .tt template A `.tt` file is a **Thor template**: ERB that Thor renders to produce the output file. When the output is itself ERB, the two layers must be told apart: - `<%= class_name %>` runs **now**, while generating; - `<%%= @kits.count %>` is escaped and becomes `<%= @kits.count %>` in the generated view. Forgetting the second `%` makes the generator try to evaluate view code at generation time and fail. ## Practical guidance - Put **cross-cutting choices** (key type, helpers, test framework) in `config.generators`, so every developer's command produces the same shape. - Override **templates** only where the team truly edits every generated file the same way, such as adding `authorize` calls or a house form layout. - Keep overrides small and review them on Rails upgrades: a new Rails version can change the original template (8.1's scaffold uses `params.expect` and `:unprocessable_content`), and an old copy in `lib/templates` silently keeps the old code. - The generated `config/application.rb` comment suggests adding `templates` to `autoload_lib`'s `ignore:` list when `lib/templates` exists, so the autoloader does not treat it as code. ## Checking that the configuration took effect - `bin/rails generate scaffold --help` lists every option with its current default, so a `--helper` default of false confirms `g.helper false` loaded. - `bin/rails generate scaffold Kit name:string --pretend` shows which files would be written, and therefore whether a template override or a hook change applied, without touching the tree. - Read one generated file after a template change; a stray `<%` that was meant to be `<%%` shows up immediately as generator output that already contains evaluated values. Together, a short `config.generators` block and a handful of `.tt` overrides are usually enough to make `bin/rails generate` produce code a reviewer accepts without edits.
- Where would you put a template so every scaffolded controller calls authorize in each action?Copy Rails' `controller.rb.tt` from the scaffold_controller generator into `lib/templates/rails/scaffold_controller/controller.rb.tt` and edit it. Rails looks there before its own templates, so every later `scaffold` or `scaffold_controller` run uses your version. Re-check it on each Rails upgrade, because your copy will not pick up changes to the original.
- Does g.helper false stop bin/rails generate controller Kits --helper from writing a helper?No. `config.generators` only changes the default value of the option. An explicit `--helper` on the command line overrides it for that run. `bin/rails generate controller --help` shows the defaults in effect, which is a quick way to confirm the configuration loaded.
- lib/templates replaces every template of a generator once one is added
- config.generators settings cannot be overridden on the command line
- In a .tt view template, <%= @kits.count %> survives into the output
- primary_key_type: :uuid changes only the kits table, not reference columns
- lib/templates overrides update themselves when Rails is upgraded
Assumes Rails 8.1, whose scaffold templates use params.expect and :unprocessable_content; a lib/templates copy taken from an older Rails keeps the older code.