skip to content

In Rails 8.1, how do legacy form_for and form_tag differ from form_with, and what changes when you migrate an old form?

level: middleimportance: should knowfreq 40%

answer

  1. Rails 5.1 unified two helpers
  2. form_for wants a record; form_tag has no builder
  3. as: becomes scope:
  4. form_for adds new_pet or edit_pet_7
  5. model: nil raises since Rails 8.0

basics

~20 s

form_for bound a record and form_tag built a model-less form with *_tag helpers; form_with, added in Rails 5.1, covers both through model:, url: and scope:. Both old helpers still work in 8.1 but are discouraged.

solid answer

~40 s

`form_for @pet` required a record or symbol, yielded a builder, generated input ids and added `class`/`id` such as `edit_pet` and `edit_pet_7` to the form tag. `form_tag(url)` yielded no builder, so fields used `text_field_tag` with bare names. Rails 5.1's `form_with` unified them: `model:` for the first case, `url:` (plus `scope:`) for the second. In 8.1, `form_for` is a thin wrapper that calls `form_with`, so verbs and `_method` behave the same. Migration is mostly mechanical: `form_for @pet` becomes `form_with model: @pet`, `as: :animal` becomes `scope: :animal`, and `form_tag path` becomes `form_with url: path`. Watch three differences: no automatic `class`/`id` on the form tag, ids and local submission depend on `load_defaults`, and since Rails 8.0 `model: nil` raises `ArgumentError`.

code

erb · 17 lines
erb
<%# Before %>
<%= form_for @pet, as: :patient do |f| %>
  <%= f.text_field :name %>
<% end %>

<%= form_tag search_pets_path, method: :get do %>
  <%= text_field_tag :q %>
<% end %>

<%# After %>
<%= form_with model: @pet, scope: :patient do |f| %>
  <%= f.text_field :name %>
<% end %>

<%= form_with url: search_pets_path, method: :get do |f| %>
  <%= f.text_field :q %>
<% end %>

go deeper

for a junior

Recall the mapping: form_for for records, form_tag for plain URLs, and form_with replacing both with model: and url:.

for a middle

Explain the differences that bite on migration: no form tag id or class, as: becoming scope:, and how load_defaults controls ids and remote forms.

for a senior

Plan an upgrade sweep: grep for form_for and form_tag, fix CSS and tests that depend on old ids, and catch controllers that pass nil now that Rails 8.0 raises.

for a principal

Decide whether a legacy-helper cleanup is worth scheduling against other upgrade work, given both helpers still run in 8.1.

## Two helpers, then one Before Rails 5.1, a Rails form started with one of two helpers: - **`form_for`** bound a record: `form_for @pet do |f|` yielded a `FormBuilder`, inferred the URL and PATCH for a persisted record, and named fields `pet[...]`. It refused `nil`, raising "First argument in form cannot contain nil or be empty". - **`form_tag`** built a form around a URL with no record: `form_tag search_path do` yielded nothing, so fields were written with the `*_tag` helpers, such as `text_field_tag :q`, and arrived unprefixed. Rails 5.1 introduced **`form_with`** to cover both with one signature: `model:`, `url:`, `scope:` and the usual `method:`, `format:` and `html` options. The Rails guides now call `form_for` and `form_tag` discouraged, but neither has been removed. In 8.1, `form_for` normalises its arguments and then delegates to `form_with` internally. ## What stays the same - The verb: a persisted record still produces PATCH via the hidden `_method` input. - The builder: `form_for` and `form_with model:` yield the same `FormBuilder` class, so `f.text_field`, `f.label` and custom builder methods work unchanged. - The token and the URL inference. ## What differs | Behaviour | `form_for @pet` | `form_with model: @pet` | |---|---|---| | `class`/`id` on the form tag | `edit_pet` / `edit_pet_7` added | none unless you pass them | | Input ids | always generated | generated when `form_with_generates_ids` is true (`load_defaults` 5.2 and later) | | Remote (XHR) submission by default | no | yes under `load_defaults` 5.1 and 6.0; local from 6.1 | | Param key override | `as: :animal` | `scope: :animal` | | Fields not on the model | error by default | allowed | | `nil` record | raises | raises `ArgumentError` since Rails 8.0 | The last row is the version trap. Earlier releases deprecated passing `nil` to `model:` and treated it like a form with no model; **Rails 8.0 removed that support**, and `form_with` now raises "Passed nil to the :model argument, expect an object or false". A view that renders `form_with model: @pet` when a controller forgot to assign `@pet` therefore fails loudly in 8.x instead of quietly rendering an empty, unscoped form. `model: false`, the default, is the intended way to say "no model". ## Migrating an old form 1. `form_for @pet do |f|` becomes `form_with model: @pet do |f|`. 2. `form_for @pet, as: :animal` becomes `form_with model: @pet, scope: :animal`; `form_with` has no `as:` option and ignores it. 3. `form_for :search, url: search_path` becomes `form_with scope: :search, url: search_path`. 4. `form_tag search_path do` with `text_field_tag :q` becomes `form_with url: search_path do |f|` with `f.text_field :q`; names stay bare because there is no scope. 5. `remote: true` on either helper belonged to rails-ujs; in a new app Turbo intercepts form submissions instead, so drop it. After migrating, check CSS or JavaScript that selected the form by the old `#edit_pet_7` id or `.edit_pet` class, and confirm the app's `load_defaults` so input ids and local submission behave as the templates expect. ## Why `form_with` won - **One signature.** Record forms and plain URL forms share the same builder API, so a search form uses `f.text_field` rather than switching to `text_field_tag`. - **Fields outside the model.** `form_with` allows builder fields for methods the object does not respond to, so a confirmation checkbox or a hint field does not need a fake accessor; `form_for` raised by default. - **Explicit options.** `scope:` and `url:` say what they do, instead of `form_for`'s mix of a symbol first argument and `as:`. ## When old helpers still appear Long-lived codebases and gems still contain `form_for` and `form_tag`. They work in 8.1, so migrating is a cleanup, not an emergency. What an interviewer wants is the mapping, the differences that can break CSS or tests, and the 8.0 `nil` change.

  • After migrating to form_with, a stylesheet rule for #edit_pet_7 stops applying. Why?
    `form_for` added `id` and `class` attributes such as `edit_pet_7` and `edit_pet` to the form tag; `form_with` adds none. Pass them explicitly with `id:` and `class:` on `form_with`, or better, give the form a stable class that does not depend on the record id.
  • Why did form_with forms submit by XHR in some older apps?
    Under `load_defaults` 5.1 and 6.0, `form_with_generates_remote_forms` was true, so forms got `data-remote="true"` and rails-ujs submitted them by XHR. `load_defaults` 6.1 switched it to false. An 8.1 app renders local forms and relies on Turbo for in-page submission.

  • form_for was removed in Rails 7, so old forms must be rewritten before upgrading.
  • form_with accepts as: just like form_for to rename the params key.
  • form_with model: nil quietly renders a form without a model in Rails 8.
  • form_tag yields a FormBuilder, so f.text_field works inside it.
  • A Rails 8.1 form_with still adds data-remote="true" by default.

Assumes Rails 8.1; form_with arrived in 5.1, its id and local-form defaults come from load_defaults 5.2 and 6.1, and Rails 8.0 removed passing nil to model:.