In Rails 8.1, what do bin/rails generate model, controller, resource and scaffold each write, and when do you pick resource over scaffold?
answer
- model: migration, class, test, fixture
- controller: actions, views, get routes
- resource: empty controller plus resources route
- scaffold: seven actions, views, JSON
- no system tests by default in 8.1
basics
~20 smodel writes a migration, model class, model test and fixture; controller writes named actions, their views, a helper, a test and get routes; resource adds an empty controller and resources route to the model files; scaffold adds a full CRUD controller, ERB views and JSON views.
solid answer
~40 s`bin/rails generate model Kit` writes `db/migrate/..._create_kits.rb`, `app/models/kit.rb`, `test/models/kit_test.rb` and `test/fixtures/kits.yml`. `generate controller Kits index show` writes the controller with those actions, one view per action, a helper, a controller test and `get "kits/index"`-style routes. `generate resource Kit` runs the model generator, then an empty `KitsController`, an empty `app/views/kits` folder, a helper, a test and `resources :kits`. `generate scaffold Kit` runs the model generator, `resources :kits`, a controller with all seven actions, the ERB views (`index`, `show`, `new`, `edit`, `_form`, `_kit`), a helper and a controller test, plus `.json.jbuilder` views when the jbuilder gem is in the Gemfile. In 8.1 scaffold writes no system test unless you pass `--system-tests=true`. Pick resource when you will write the actions yourself or the views would be thrown away.
code
bash · 5 linesbin/rails generate model Kit name:string # migration, model, test, fixture
bin/rails generate controller Dashboard show # action, view, helper, test, get route
bin/rails generate resource Kit name:string # model files + empty controller + resources :kits
bin/rails generate scaffold Kit name:string price:decimal supplier:references
bin/rails generate scaffold Kit name:string --system-tests=true # opt back ingo deeper
Know which files model, controller, resource and scaffold write, and that scaffold gives a working CRUD screen once the migration runs.
Explain how the generators stack: resource is model plus empty controller plus route, scaffold swaps in scaffold_controller. Know the jbuilder and system-test details.
Judge when generated code helps: scaffold for prototypes and internal tools, resource or hand-written controllers for product code that needs authorization and real forms.
Set the team's stance on generators: which ones are encouraged, how their output is reviewed, and whether templates are customized so generated code already matches house style.
## Four generators, layered on each other Rails' code generators are **Thor** classes under `railties/lib/rails/generators` (plus the ORM's own under `activerecord/lib/rails/generators`). The four you use most build on one another, which explains what each writes: | Generator | Built from | Main output | |---|---|---| | `model` | the ORM generator (`active_record`) and the test framework | migration, model, model test, fixture | | `controller` | itself, the template engine, helper and test framework | controller with named actions, one view per action, helper, test, `get` routes | | `resource` | `model` + an empty controller + `resource_route` | everything `model` writes, an empty controller, `resources :kits` | | `scaffold` | `resource` with `scaffold_controller` swapped in | everything `resource` writes, seven actions, full views, JSON views | For a meal-kit app the commands look like `bin/rails generate scaffold Kit name:string price:decimal supplier:references`; the attribute list is passed down to the model, the views and the tests. ## generate model The model generator delegates to the configured ORM (Active Record by default) and the test framework (Minitest's `test_unit` by default): - `db/migrate/<timestamp>_create_kits.rb` with a `create_table :kits` and `t.timestamps`; - `app/models/kit.rb`, a subclass of `ApplicationRecord`, with `belongs_to` lines for `references` attributes; - `test/models/kit_test.rb` and `test/fixtures/kits.yml`. `--parent` changes the superclass (and skips the migration, for single-table inheritance), and `admin/kit` writes a namespaced model with a `table_name_prefix` module. ## generate controller `bin/rails generate controller Kits index show` writes `KitsController` with empty `index` and `show` methods, `app/views/kits/index.html.erb` and `show.html.erb`, `app/helpers/kits_helper.rb`, `test/controllers/kits_controller_test.rb`, and one route per action, `get "kits/index"` and `get "kits/show"`. Those are plain `get` routes, not resourceful ones; `--skip-routes` leaves `config/routes.rb` alone. ## generate resource `resource` is the model generator plus a **thin web layer**: - the model, migration, test and fixture; - an **empty** `KitsController` and an empty `app/views/kits` directory; - the helper and controller test; - `resources :kits` in `config/routes.rb`. Its USAGE text says it outright: unlike scaffold it creates no views and adds no methods to the controller. ## generate scaffold `scaffold` swaps the empty controller for `scaffold_controller`, which writes a working CRUD slice: 1. a controller with `index`, `show`, `new`, `edit`, `create`, `update` and `destroy`, a `before_action` that loads the record, and a private params method; 2. ERB views `index`, `show`, `new`, `edit`, the shared `_form` and the `_kit` partial; 3. a controller test, and a system test **only** with `--system-tests=true` (Rails 8.1 stopped generating them by default); 4. with the **jbuilder** gem present, as it is in a new full-stack app's Gemfile, `index.json.jbuilder`, `show.json.jbuilder`, `_kit.json.jbuilder` and `respond_to` blocks for JSON. `--api` writes an API controller and no views. In an app created with `--api`, `config.api_only` makes the generators skip views and helpers. ## Resource or scaffold? - **Scaffold** fits a prototype, an internal admin screen, or learning what a conventional CRUD slice looks like. Expect to rewrite the views, add authorization, and replace the `text_field` for `supplier_id` with a real select. - **Resource** fits when the controller's behaviour is specific (a checkout flow, a JSON endpoint you will shape by hand) and generated views would be deleted anyway. - **Model** alone fits domain objects with no web endpoint of their own. - **Controller** alone fits pages that are not a resource, such as a dashboard. Whichever you run, read the generated code before committing it: generators write conventions, not decisions. ## Reading the generator output The log a generator prints is a map of what happened, indented by which sub-generator did it: - `invoke active_record` then `create db/migrate/...` and `create app/models/kit.rb`: the ORM hook; - `invoke test_unit` then the test and fixture lines: the test framework hook; - `invoke resource_route` then `route resources :kits`: the routing hook; - `invoke scaffold_controller`, `invoke erb`, `invoke helper`, `invoke jbuilder`: the web layer. Each `invoke` line names a hook you can switch off with `--skip-<hook>` or `--no-<hook>`, which is how you trim scaffold's output without editing anything afterwards. Running the command with `--pretend` first prints the same log without writing files, which is the cheapest way to answer "what would this generate?" in a real codebase.
- Why does bin/rails generate controller Kits index add get "kits/index" instead of resources :kits?The controller generator only knows the action names you listed, so it adds one `get` route per action and nothing else. Resourceful routing comes from the `resource_route` generator, which `resource` and `scaffold` invoke. Pass `--skip-routes` if you will write the routes yourself.
- What changed about scaffold's tests in Rails 8.1, and how do you get the old output back?Scaffold still writes a controller test, but it no longer writes `test/system/kits_test.rb` by default. Pass `--system-tests=true` to get one, or generate a system test separately with `bin/rails generate system_test kits`. The change keeps slow browser tests out of every new scaffold.
- resource generates the same views as scaffold, minus the tests
- generate controller Kits index adds resources :kits to the routes
- scaffold in Rails 8.1 always writes a system test
- generate model also writes a controller and a route
- scaffold's JSON views come from Rails itself, with or without jbuilder
Assumes Rails 8.1, whose scaffold generator writes no system tests unless --system-tests=true is passed.