With Turbo Frames, how do target="_top" and data-turbo-frame decide whether a link or form updates its own frame, another frame or the whole page?
answer
- default: the enclosing frame
- target on the turbo-frame
- data-turbo-frame on link or form
- _top breaks out to the page
- outside links can target a frame
basics
~20 sInside a frame, links and forms update that frame by default. target on the <turbo-frame> sets a default for its descendants, data-turbo-frame on a single link or form overrides it, and "_top" in either sends navigation to the whole page.
solid answer
~40 sNavigation inside a `<turbo-frame>` targets that frame unless told otherwise. The frame's own `target` attribute sets the default for everything inside it: another frame's id, or `_top` for full-page Turbo Drive visits. A link, form or submit button can override that with `data-turbo-frame`, which takes the same values; Turbo reads the submitter first, then the element, then the frame's `target`. `data-turbo-frame` also works outside any frame, so a search form at the top of a page can update `<turbo-frame id="results">` further down. In Rails these are written as `turbo_frame_tag "contacts", target: "_top"` and `link_to ..., data: { turbo_frame: "_top" }`. The classic use is a list frame whose rows link to full pages: without `_top`, Turbo would look for the list frame's id in the show page and display "Content missing".
code
erb · 12 lines<%= turbo_frame_tag "favourites", src: favourites_contacts_path, target: "_top" %>
<%= form_with url: search_contacts_path, method: :get,
data: { turbo_frame: "results" } do |form| %>
<%= form.search_field :q %>
<% end %>
<%= turbo_frame_tag "results" %>
<%= turbo_frame_tag contact do %>
<%= link_to "Edit", edit_contact_path(contact) %>
<%= link_to "Open", contact, data: { turbo_frame: "_top" } %>
<% end %>go deeper
Know that links inside a frame update that frame, and that data-turbo-frame="_top" or target="_top" sends them to the whole page instead.
Explain the precedence of submitter, element and frame target, how data-turbo-frame lets outside forms target a frame, and why missing targets lead to Content missing.
Design frame targets for search, master-detail and modal layouts so each navigation lands in the right region, and keep frame ids consistent across the pages that answer them.
Keep navigation rules declarative in markup so the routing of every interaction stays reviewable without reading client code.
## The default: the enclosing frame When a link or form sits inside a `<turbo-frame>`, Turbo assumes the navigation belongs to that frame. It requests the URL with a `Turbo-Frame` header, extracts the frame with the same id from the response and swaps its contents in. That default is right for in-place editing and wrong for a list whose rows link to full pages. ## The two controls | Where | Attribute | Effect | |---|---|---| | on the `<turbo-frame>` | `target="_top"` | every link and form inside navigates the whole page by default | | on the `<turbo-frame>` | `target="other_frame"` | links inside navigate the frame with that id | | on an `<a>`, `<form>` or submit button | `data-turbo-frame="_top"` | this one element breaks out to a full-page visit | | on an `<a>`, `<form>` or submit button | `data-turbo-frame="some_frame"` | this element navigates that frame, even from outside any frame | The precedence Turbo uses when a navigation starts inside a frame: the submitter's `data-turbo-frame`, then the link's or form's `data-turbo-frame`, then the frame's `target`, and finally the enclosing frame itself. If the named frame does not exist on the page, the enclosing frame is used. A frame with the `disabled` attribute never navigates. In Rails: - `turbo_frame_tag "contacts", target: "_top"` renders the frame-level default; - `link_to "Open", contact, data: { turbo_frame: "_top" }` renders `data-turbo-frame="_top"`; - `form_with url: search_contacts_path, method: :get, data: { turbo_frame: "results" }` points a search form at the results frame. ## Breaking out with _top An address book that shows a lazily loaded "Favourites" tray in a frame illustrates the problem. Each contact in the tray links to the contact's full page. Without a target, a click makes Turbo request `/contacts/42` and look for `<turbo-frame id="favourites">` in it; the show page has no such frame, so the tray displays "Content missing". Marking the tray `target="_top"` turns those clicks into ordinary Turbo Drive visits. `_top` is a Turbo Drive visit, not a browser reload: the body is swapped and history is pushed as usual. To leave Turbo entirely (a file download, a third-party page), `data-turbo="false"` is the tool instead. ## Targeting another frame `data-turbo-frame` is not limited to elements inside frames. Turbo also watches links and forms anywhere on the page that name a frame and redirects their navigation into it. Common layouts: 1. **Search and results.** A GET search form in the header carries `data-turbo-frame="results"`; the response renders the full search page, Turbo extracts `results`, and the header keeps its typed text. 2. **Master and detail.** A list on the left links to contacts with `data-turbo-frame="contact_detail"`; the right-hand frame shows the selected contact. 3. **A modal frame.** An empty `<turbo-frame id="modal">` in the layout receives "New contact" links that target it. In each case the server renders a normal page that contains the target frame; only the frame id ties request and response together. ## Interaction with history Frame navigation does not change the URL or push history by default, whichever frame it targets. If a targeted frame's state should be shareable (the selected contact, a search query), add `data-turbo-action="advance"` so the frame navigation is promoted to a page visit; with `_top` that is unnecessary, because the whole page navigates anyway. ## Seeing which frame a request targets The browser's network panel answers most targeting questions. A request that navigates a frame carries the header `Turbo-Frame` with the frame's id, so a search that should fill `results` must show `Turbo-Frame: results`. A `_top` navigation is an ordinary Turbo Drive visit and sends no such header. On the server, `turbo_frame_request_id` exposes the same value, which helps when one action answers several frames. ## Common mistakes - Leaving list frames without `target="_top"`, so every row link ends in "Content missing". - Using `target="_blank"` on a link expecting a frame behaviour: Turbo ignores links with a `target` other than `_self` and the browser opens a new tab. - Putting `data-turbo-frame` on the `<turbo-frame>` itself, where Turbo reads `target` instead. - Naming a frame id that exists on some pages and not others, silently falling back to the enclosing frame.
- In which order does Turbo decide which frame a form submission navigates?It checks `data-turbo-frame` on the submit button first, then on the form, then the enclosing frame's `target`. If the named frame is `_top`, Turbo makes a page visit; if the named frame is not on the page, the enclosing frame is used.
- How does a frame behave when Turbo.session.drive = false?Links and forms inside a frame still navigate the frame, because Turbo treats elements within a frame as navigable even with Drive disabled. The switch makes full-page visits from elements outside frames opt-in.
- target="_top" on a frame makes its links do a full browser reload without Turbo.
- data-turbo-frame only works on links that are already inside a turbo-frame.
- target="_blank" on a link inside a frame opens the page inside that frame.
- A frame's target attribute overrides data-turbo-frame on individual links.
- Targeting another frame pushes the new URL into the browser history by default.