In a Rails form_with block, what HTML do f.label, f.text_field, f.email_field, f.textarea and f.hidden_field produce for a registration?
answer
- object name plus attribute
- name="registration[email]"
- id registration_email, label for= matches
- value read from the record
- textarea primary, text_area alias
basics
~20 sEach builder helper names its input registration[attribute], gives it the id registration_attribute, and fills it from the record. f.label points for= at that id and looks its text up through I18n, falling back to the attribute's human name.
solid answer
~30 sWith `form_with model: @registration`, every builder helper combines the object name and the attribute. `f.text_field :name` renders `<input type="text" name="registration[name]" id="registration_name" value="...">`, so the controller reads `params[:registration][:name]`. `f.email_field` is the same with `type="email"`; `f.hidden_field` with `type="hidden"`; `f.textarea :bio` puts the value between `<textarea>` tags and accepts `size: "60x8"` for cols and rows. Values come from the record, using the raw user input after a failed save. `f.label :email` writes `for="registration_email"` and takes its text from `helpers.label.registration.email`, then `human_attribute_name`, so translations rename labels without template edits. In Rails 8, `textarea` is the primary name and `text_area` remains an alias.
code
erb · 13 lines<%= form_with model: @registration do |f| %>
<%= f.label :name %>
<%= f.text_field :name, autocomplete: "name" %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :bio, "Short bio" %>
<%= f.textarea :bio, size: "60x8" %>
<%= f.hidden_field :ticket_code %>
<%= f.submit %>
<% end %>go deeper
Recall the pattern: object name plus attribute gives name registration[email] and id registration_email, and the value comes from the record.
Explain the label lookup order through helpers.label and human_attribute_name, and why fields show raw user input after a failed save.
Show judgment about hidden fields as untrusted input and about keeping label text in locale files so copy changes avoid template edits.
Weigh consistency of naming and translation across many forms against one-off markup, and when shared builder conventions are worth enforcing.
## What a field helper is responsible for Inside `form_with model: @registration do |f|`, the block variable `f` is a **form builder** bound to the record and to the **object name** `registration` (the model's `param_key`). Every field helper on it does three jobs: 1. **Naming**: the `name` attribute is `registration[attribute]`, which Rails' parameter parser turns into a nested hash, `params[:registration][:attribute]`. 2. **Identifying**: the `id` is `registration_attribute`, so a label and the input can point at each other. A new app's `load_defaults` makes `form_with` generate these ids. 3. **Filling**: the `value` comes from the bound record, so an edit page shows current data. ## The common helpers side by side For a conference registration with a name, email, short bio and a hidden ticket code: | Builder call | Element written | Value source | |---|---|---| | `f.text_field :name` | `<input type="text" name="registration[name]" id="registration_name">` | `@registration.name` | | `f.email_field :email` | `<input type="email" ...>` | `@registration.email` | | `f.textarea :bio, size: "60x8"` | `<textarea cols="60" rows="8" ...>` with the text inside | `@registration.bio` | | `f.hidden_field :ticket_code` | `<input type="hidden" ...>` | `@registration.ticket_code` | | `f.label :email` | `<label for="registration_email">Email</label>` | I18n lookup | `email_field` adds nothing but the input type; the browser decides what that type changes for the user. Any extra option you pass, such as `autocomplete: "email"` or `maxlength: 80`, becomes an HTML attribute, and a `maxlength` also sets `size` unless you pass one. ## Where label text comes from `f.label :email` with no text argument looks up, in order: 1. `helpers.label.registration.email` in the locale files; 2. the model's `human_attribute_name(:email)`, which reads `activerecord.attributes.registration.email`; 3. the attribute name humanized, `"Email"`. Passing a string, `f.label :email, "Work email"`, or a block skips the lookup. Because the label's `for` uses the same id the field generates, the pair stays connected as long as both come from the same builder. `placeholder: true` on a text field runs a similar lookup under `helpers.placeholder`. ## Values after a failed save Text-like fields read the attribute's **before-type-cast** value when that value came from the user. If someone types `twelve` into a number of guests stored as an integer, the cast value is `nil`, but after validation fails the field redisplays `twelve`, so the user can see and fix what they typed instead of an empty box. ## Hidden fields are not secret `f.hidden_field :ticket_code` keeps a value in the page without showing it, but the browser sends whatever the page holds, and anyone can edit it before submitting. Treat it as user input: never put a price, a role or an owner id there and trust it on the way back. ## The model-less counterparts Each builder helper has a `*_tag` sibling in `FormTagHelper` for fields that belong to no record, such as a search box or a one-off token: - `text_field_tag :q, params[:q]` writes `name="q"` and `id="q"` with exactly the value you pass. - `email_field_tag`, `hidden_field_tag` and `textarea_tag` work the same way; `label_tag :q, "Search"` writes `for="q"`. - Nothing is read from a record and no object-name prefix is added, so you control the name completely. Inside `form_with`, the builder methods are usually the better choice even without a model: `form_with url: search_path do |f| f.text_field :q end` gives the same bare name while keeping one style of call across the app. ## Names in Rails 8 Rails 8 renamed a few helpers to match HTML: **`textarea`** is now the primary method and `text_area` an alias, alongside `checkbox` and `check_box`. Both spellings work in 8.1; generators and current guides use the new ones, so seeing `text_area` in a codebase marks older templates, not a bug. ## What interviewers check - That you can predict `name` and `id` from the object name and attribute without running the code. - That you know the controller reads `params[:registration]` because of the naming. - That you know labels translate through I18n and do not need hard-coded text. - That you treat hidden fields as editable input.
- Why does f.label :email still show "Email" when no translation exists?The label helper tries `helpers.label.registration.email`, then the model's `human_attribute_name(:email)`, and finally humanizes the attribute name. `human_attribute_name` itself falls back to a humanized name when `activerecord.attributes.registration.email` is missing, so the label always has text.
- How do you render a field for a method the model does not have, such as a confirmation box?`form_with` allows builder fields for names the object does not respond to; the field simply renders with no value, as `registration[accept_code_of_conduct]`. The controller then has to handle that key itself, or the model needs a virtual attribute to receive it.
- f.text_field :name renders name="name", and Rails maps it to the model by id.
- The label's for attribute must be written by hand to match the input.
- A hidden_field value is safe to trust because users cannot see it.
- text_area was removed in Rails 8, so old templates break.
- After a failed save the field shows the type-cast value, so invalid input disappears.
Assumes Rails 8.1, where textarea and checkbox are the primary helper names and text_area and check_box remain aliases.