With Turbo Frames, what does data-turbo-action="advance" change for a paginated contacts frame, and what must the Rails action then guarantee?
answer
- frame navigation leaves the URL alone
- advance pushes, replace swaps
- frame src and address bar both change
- a reload renders the full page
- state must come from params
basics
~20 sdata-turbo-action="advance" promotes a frame navigation to a page visit: the frame updates and the browser URL moves to the frame's new URL with a history entry. The Rails action must render that URL correctly as a full page.
solid answer
~40 sBy default a frame navigation changes only the frame: the address bar, history and Back button stay where they were, so page 3 of a contacts list cannot be bookmarked or shared. Adding `data-turbo-action="advance"` to the `<turbo-frame>`, a link or form that navigates it, or a submit button promotes the navigation: Turbo updates the frame and pushes the frame's new URL onto history, so `/contacts?page=3` appears in the address bar. `replace` updates the URL without adding an entry. The obligation moves to the server: a reload, a shared link or a new tab requests `/contacts?page=3` as a full page with no `Turbo-Frame` header, so the action must build page 3, filters and all, from `params` and render it in the full layout. Back restores the previous state from Turbo's snapshot cache.
code
erb · 6 lines<%# app/views/contacts/index.html.erb %>
<h1>Contacts</h1>
<%= turbo_frame_tag "contacts", data: { turbo_action: "advance" } do %>
<%= render @contacts %>
<%= link_to "Next", contacts_path(page: params.fetch(:page, 1).to_i + 1, q: params[:q]) %>
<% end %>go deeper
Know that a normal frame navigation does not change the URL, and that data-turbo-action="advance" makes the frame's URL show in the address bar.
Explain advance versus replace, where the attribute can be declared, and why a reload of the promoted URL must be rendered as a full page from params.
Decide which frame navigations deserve a URL, and test reloads and shared links so list state survives outside the frame.
Treat URLs as the contract for shareable state: every promoted frame navigation commits the server to rendering that state directly.
## Frames do not touch history by default A Turbo Frame navigation replaces the frame's contents and updates the frame's `src`, but it leaves the browser's URL and history alone. For an in-place edit form that is exactly right. For a paginated or filtered list it is a problem: - the address bar still shows `/contacts` while the frame shows page 3; - a reload goes back to page 1; - the Back button leaves the page instead of returning to page 2; - the user cannot copy a link to what they see. ## Promoting the navigation `data-turbo-action` accepts the visit actions `advance` and `replace`. Turbo reads it from the submitter, the link or form, or the frame itself, so it can be declared: - on the `<turbo-frame>`, for every navigation inside it; - on an individual `<a>` or `<form>` that navigates the frame; - on a submit button inside such a form. With `advance`, a click on "Next" in `<turbo-frame id="contacts" data-turbo-action="advance">` sets both the frame's `src` and the browser path to `/contacts?page=3`, pushing a history entry. With `replace`, the URL changes but the current entry is overwritten, which suits a filter that updates on every keystroke. | Setting | Frame content | Address bar | History | |---|---|---|---| | none | updated | unchanged | unchanged | | `data-turbo-action="advance"` | updated | frame URL | new entry pushed | | `data-turbo-action="replace"` | updated | frame URL | current entry replaced | Turbo still renders only the frame, keeping the rest of the page and its scroll position. It also snapshots the page so that Back can restore the previous frame contents from its cache. ## The server's obligation Once the URL is in the address bar, anyone can request it without the frame: a reload, a bookmark, a link pasted in chat, a new tab. Those requests arrive without the `Turbo-Frame` header and expect a whole page. The Turbo handbook states it plainly: it is the application's responsibility to render the second page, and any other state derived from the URL, when the page is reloaded. In a Rails app that means: 1. The index action reads every piece of list state from `params`: page, sort, filter, search query. 2. The full view renders the frame with those params applied, not the default first page. 3. Links inside the frame are built from the same params, so the next page keeps the filter. ```ruby def index @contacts = Contact.where(archived: false).order(:name).page(params[:page]) end ``` (The pagination call depends on the gem in use; what matters is that it reads `params[:page]`.) If the index action ignores `params[:page]` when there is no frame header, the bug only appears on reload, which is why it slips through manual testing that always clicks from page 1. ## When not to advance Not every frame navigation deserves a URL: - **Inline edit forms** should not push `/contacts/42/edit` into history; Back would land on an edit form the user never "visited". - **Modals** loaded into a frame usually should not change the URL unless they are meant to be linkable. - **Lazy frames** loaded from `src` are background loading, not navigation. A useful test: if a colleague opened the URL in a new tab, would they expect to see this state? If yes, advance; if not, leave the frame navigation silent. ## Testing a promoted frame Four manual checks catch nearly every bug in this area, and the same steps translate directly into a system test: 1. Click through to page 3 and confirm the address bar shows the page-3 URL. 2. Reload and confirm the page renders page 3 with the same filter. 3. Open that URL in a new tab and confirm the same result. 4. Press Back and confirm the frame returns to page 2 rather than leaving the page. ## Related mechanics - `Turbo.visit(url, { frame: "contacts", action: "advance" })` performs the same promotion from JavaScript. - A frame whose `target` is `_top` needs none of this, because its links already navigate the whole page. - Promoted frame navigations still send the `Turbo-Frame` header, so turbo-rails renders them with the minimal frame layout; only direct requests get the full layout.
- When would you use data-turbo-action="replace" on a frame instead of advance?When the URL should reflect the frame's state but each change is not worth a Back step, such as a live filter that navigates on every keystroke. `replace` updates the address bar while overwriting the current history entry.
- After an advance frame navigation, what does a reload of that URL send to Rails?A normal full-page GET for the frame's URL, without the `Turbo-Frame` header. turbo-rails renders the application layout, and the action must reproduce the frame's state from the URL's params.
- Frame navigations always update the browser URL, so advance is only for styling.
- After a reload, Turbo restores the frame's page 3 from its cache automatically.
- data-turbo-action="advance" makes the link navigate the whole page instead of the frame.
- The index action can ignore params[:page] when turbo_frame_request? is false.
- Every frame navigation, including inline edit forms, should advance history.