In Action View, what does dom_id return for a saved record, a new record and a prefix argument, and why use it?
answer
- param_key, underscore, primary key
- new_ when there is no key
- prefix goes in front
- dom_class drops the id
- dom_target arrived in 8.1
basics
~10 sdom_id(listing) returns "listing_42"; for an unsaved record or the class it returns "new_listing"; dom_id(listing, :edit) returns "edit_listing_42". One naming rule lets views, Turbo Stream targets, forms and tests address the same element.
solid answer
~40 s`dom_id(record, prefix = nil)` joins the model's `param_key` and the record's key with an underscore: `dom_id(listing)` is `"listing_42"`. When there is no key, an unsaved record or the class itself, it returns `"new_listing"`. A prefix goes in front: `dom_id(listing, :edit)` is `"edit_listing_42"` and `dom_id(Listing, :search)` is `"search_listing"`. `dom_class(listing)` gives `"listing"` without the id, and passing `nil` raises `ArgumentError`. Its value is the shared convention: a partial renders `id="<%= dom_id(listing) %>"`, `form_with(model: listing)` derives its id from the same rule, Turbo's stream helpers target records by the same id, and a system test can scope with `within("#" + dom_id(listing))`. Rails 8.1 adds `dom_target(listing, :price)`, giving `"listing_42_price"` for sub-elements. Outside views, `ActionView::RecordIdentifier.dom_id` works directly.
code
erb · 9 lines<%# app/views/listings/_listing.html.erb %>
<article id="<%= dom_id(listing) %>" class="<%= dom_class(listing) %>">
<h2><%= listing.title %></h2>
<p id="<%= dom_target(listing, :price) %>">
<%= number_to_currency(listing.price) %>
</p>
</article>
<%# => <article id="listing_42" class="listing">
<p id="listing_42_price">$24.00</p> %>go deeper
Recall the three shapes: listing_42, new_listing, and edit_listing_42 with a prefix.
Explain where the parts come from (param_key, to_key), the new_ fallback, dom_class, and why Turbo and forms rely on the same rule.
Remove hand-built ids that drift from dom_id, use dom_target for sub-elements in 8.1, and reuse the identifier in tests and broadcasts.
Treat DOM ids as a contract between server templates, streams and front-end code, and keep one generator for them across the app.
## The convention dom_id encodes On a vinyl listing page every card, price and delete button belongs to one `Listing`. Code in several places needs to find that card again: the template that renders it, a Turbo Stream that replaces it after an edit, a Stimulus controller, a system test. **`dom_id`** gives all of them the same id without anyone concatenating strings. It lives in `ActionView::RecordIdentifier`, which every view includes: ```ruby dom_id(Listing.find(42)) # => "listing_42" dom_id(Listing.new) # => "new_listing" dom_id(Listing) # => "new_listing" dom_id(Listing.find(42), :edit) # => "edit_listing_42" dom_id(Listing, :search) # => "search_listing" dom_class(Listing.find(42)) # => "listing" ``` ## How the string is built 1. The base is the model's **`param_key`** from `model_name`, the singular underscored name: `listing`, or `catalog_listing` for a plain `Catalog::Listing` model. 2. The record's **`to_key`** is joined with underscores. A saved record gives `[42]`; a composite primary key gives several parts, such as `listing_7_42`. 3. If there is **no key**, because the record is unsaved or you passed the class, the prefix defaults to **`new`**, which gives `new_listing`. 4. A **prefix** argument is put in front with an underscore and replaces `new` for keyless records. 5. `nil` is rejected with `ArgumentError`, which catches a view rendering an id for a missing record. `dom_class(record_or_class, prefix = nil)` is the same name without the id, useful as a CSS class shared by every card. ## Why not build the id by hand - **One rule everywhere.** `form_with(model: listing)` names its form `edit_listing_42` or `new_listing` with the same record identifier. Turbo's Rails helpers derive stream targets and frame ids from `dom_id` when they get a record. A hand-written `"listing-#{listing.id}"` silently stops matching them. - **Model renames follow.** The id comes from `model_name`, so it changes with the class instead of drifting from it. - **Unsaved records are handled.** The `new_` form gives a stable id for the form or placeholder of a record that has no key yet. - **Tests read like the view.** A system test can scope to one card with `within("#" + dom_id(listing))` instead of repeating the format. | Helper | Example | Result | |---|---|---| | `dom_id(listing)` | saved, id 42 | `listing_42` | | `dom_id(listing, :price)` | prefix | `price_listing_42` | | `dom_class(listing, :sold)` | class with prefix | `sold_listing` | | `dom_target(listing, :price)` | Rails 8.1 | `listing_42_price` | ## dom_target in Rails 8.1 `dom_target(*objects)` joins any number of parts with underscores, passing records through `dom_id`, classes through `dom_class` and keeping strings and symbols. It reads left to right, so sub-elements of a card get ids such as `listing_42_price` or, with two records, `listing_42_comment_1`. Before 8.1 the same result needed a prefix (`price_listing_42`, read right to left) or string building. ## Using it outside a view `ActionView::RecordIdentifier` extends itself, so a model, job or presenter can call `ActionView::RecordIdentifier.dom_id(listing)` or include the module. Controllers can also go through their `helpers` proxy. The rule stays identical wherever the id is computed. ## Mistakes interviewers listen for - **Hyphens instead of underscores.** Ids are `listing_42`, not `listing-42`; CSS and JavaScript written against a hand-made format will not match. - **Table name instead of model name.** The base is the singular `param_key` (`listing`), not the table (`listings`). - **Prefix position.** `dom_id(listing, :edit)` is `edit_listing_42`; only `dom_target` appends. - **Assuming nil is harmless.** `dom_id(nil)` raises `ArgumentError`, which usually points at a view rendering a record that was never loaded. - **Duplicated ids.** Rendering the same record twice on one page (a card and a sidebar entry) produces two elements with one id; give one of them a prefix or a `dom_target` suffix so streams and tests hit the right one.
- What does dom_id return for a record with a composite primary key?It joins every part of `to_key` with underscores after the param key, so a listing keyed by `[7, 42]` gives `"listing_7_42"`. If any part is missing the record is treated as keyless and you get `"new_listing"`.
- How is dom_target different from dom_id with a prefix?`dom_id(listing, :price)` puts the prefix in front, `"price_listing_42"`, and accepts one record. `dom_target(listing, :price)`, new in Rails 8.1, joins any number of records, classes, strings and symbols left to right, `"listing_42_price"`.
- dom_id returns "listing-42" with a hyphen
- dom_id(nil) quietly returns an empty string
- dom_id(listing, :edit) appends the prefix: "listing_42_edit"
- dom_id for an unsaved record returns "listing_" with a blank id
- dom_id uses the table name, so it returns "listings_42"
Assumes Rails 8.1, where dom_target was added; dom_id and dom_class behave the same in earlier releases.