In a Rails app serving Hotwire Native, what do hotwire_native_app? and refresh_or_redirect_to do after a habit is created in a native modal?
answer
- user agent matches (Turbo|Hotwire) Native
- recede, resume, refresh historical locations
- native rules: pop, none, refresh
- modal dismissed first, then the action
- notice travels as a query parameter
basics
~20 shotwire_native_app? checks the user agent for Hotwire Native or Turbo Native. refresh_or_redirect_to sends a native app to /refresh_historical_location, which dismisses the modal and reloads the screen beneath; a browser gets an ordinary redirect to the given URL.
solid answer
~40 sturbo-rails includes `Turbo::Native::Navigation` in every `ActionController::Base` controller. `hotwire_native_app?`, also a view helper and aliased as `turbo_native_app?`, matches `Turbo Native` or `Hotwire Native` in the user agent. The three navigation helpers take a URL for browsers: `recede_or_redirect_to`, `resume_or_redirect_to` and `refresh_or_redirect_to`. For a native request they redirect to `/recede_historical_location`, `/resume_historical_location` or `/refresh_historical_location`, routes turbo-rails draws. The native libraries carry built-in rules for those paths: any modal is dismissed first, then recede pops the visible screen, resume does nothing more, and refresh reloads the visible screen. So after a habit is saved in a modal, `refresh_or_redirect_to habits_path, notice: "Habit created"` closes the modal and reloads whatever list the user came from, while browsers go to /habits with the flash. The native branch passes only `notice:` and `native_params:`, as query parameters.
code
ruby · 16 linesclass HabitsController < ApplicationController
def create
@habit = Current.user.habits.new(params.expect(habit: [ :name, :cadence ]))
if @habit.save
refresh_or_redirect_to habits_path, notice: "Habit created"
else
render :new, status: :unprocessable_content
end
end
def destroy
Current.user.habits.find(params[:id]).destroy
recede_or_redirect_to habits_path, notice: "Habit deleted"
end
endgo deeper
Recall that hotwire_native_app? detects the native shell and is usable in views and controllers.
Explain the three historical-location redirects, what each does natively and the browser fallback.
Choose recede, resume or refresh per action so modal flows end where the user started, and handle the notice and native_params quirks.
Keep controllers expressing navigation intent rather than platform-specific pages, so one Rails codebase serves browsers and both shells.
## Detecting the native app turbo-rails mixes `Turbo::Native::Navigation` into `ActionController::Base` (not `ActionController::API`). It provides: - **`hotwire_native_app?`**: true when `request.user_agent` matches `/(Turbo|Hotwire) Native/`. Hotwire Native web views put `Hotwire Native iOS;` or `Hotwire Native Android;` in their user agent, and older Turbo Native apps sent `Turbo Native`. - **`turbo_native_app?`**: an alias kept for code written against the predecessors. - Both are registered with `helper_method`, so layouts can use them to drop web-only chrome such as a top navigation bar the native shell already provides. ## The problem the navigation helpers solve In a browser, the end of a successful form submission is a redirect to some page. In a native shell, a form often lives in a **modal**, opened from whatever screen the user was on: the habit list, today's check-ins, a streak view. A plain `redirect_to habits_path` makes the app visit `/habits`: the modal closes and, if the screen underneath was something else, `/habits` is pushed on top of it, giving the user a stack they never navigated. What the app should do is "close the form and show me where I was, updated". The server cannot know which screen that is, so it sends an **intent** instead of a page. ## The three intents | helper | native request | browser request | |---|---|---| | `recede_or_redirect_to(url)` | redirect to `/recede_historical_location` | `redirect_to url` | | `resume_or_redirect_to(url)` | redirect to `/resume_historical_location` | `redirect_to url` | | `refresh_or_redirect_to(url)` | redirect to `/refresh_historical_location` | `redirect_to url` | Each has a `_back_or_to` variant (`refresh_or_redirect_back_or_to`) whose browser branch uses `redirect_back` with the URL as fallback. The three paths are ordinary GET routes drawn by turbo-rails (unless `config.turbo.draw_routes` is false), answering with a short HTML placeholder such as "Refreshing…". The point is the URL: the native libraries ship built-in path rules for them, appended after the app's own rules so they always win, and treat them as **historical locations**: 1. If a modal is presented, it is dismissed first. 2. Then recede **pops** the visible screen, resume does **nothing** more, and refresh **reloads** the visible screen without pushing anything. For a habit created in a modal, refresh is usually right: the modal closes and the list or day view underneath reloads with the new habit. Resume suits a cancel button or an unchanged record, and recede suits "done here, go back one level", as after deleting the habit whose page you were on. ## Options on the native branch `notice:` is the only standard option the native branch forwards, and it travels as a **query parameter** on the historical URL, not as flash: `/refresh_historical_location?notice=Habit+created`. Extra values go through `native_params:`, which the browser branch never sees. Options such as `alert:` or `status:` apply only to the browser redirect. If the native app should show a toast, read the parameter on the native side or show the message on the refreshed page. ## Choosing per action in the habit tracker | action | browser | native intent | |---|---|---| | create habit from a modal | redirect to the list | `refresh`: close the modal, reload the screen underneath | | cancel editing | redirect to the habit | `resume`: close the modal, change nothing | | delete the habit being viewed | redirect to the list | `recede`: close any modal, pop the habit screen | | failed save | render the form with 422 | none: the modal re-renders the form in place | A failed save never uses these helpers: the form is re-rendered with errors inside the same screen, exactly as on the web. ## Testing it turbo-rails' own tests show the pattern: send the request once plainly and assert the normal redirect, then again with a `User-Agent` containing `Hotwire Native` and assert the redirect to the matching historical-location URL. An integration test per create, update and destroy action covers both branches. ## Which app versions handle it The Hotwire Native documentation states that the iOS and Android libraries handle the historical-location URLs automatically from version 1.2.0; apps built on the Turbo Native predecessors needed equivalent rules in their own path configuration.
- When would resume_or_redirect_to fit better than refresh_or_redirect_to?When nothing on the underlying screen changed, such as a cancel action or saving a draft that list does not show. Resume only dismisses the modal and leaves the screen beneath as it is, avoiding a reload; refresh dismisses the modal and reloads that screen.
- Why is the notice missing from flash in the native app?The native branch redirects to the historical-location URL with `notice` as a query parameter rather than setting flash, and the app never renders that placeholder page. Show the message natively from the parameter, or render it on the refreshed screen.
- Can an API-only Rails app use these helpers?Not without extra work: turbo-rails includes `Turbo::Native::Navigation` when `ActionController::Base` loads, so controllers inheriting from `ActionController::API` do not get it. A Hotwire Native app expects HTML pages anyway, which an API-only app does not render.
- hotwire_native_app? checks a custom request header that the native app sets
- refresh_or_redirect_to renders a Turbo Stream refresh action for native apps
- recede, resume and refresh behave the same for browsers and native apps
- The notice: option sets flash for the native app's next page
- A plain redirect_to always returns a native modal to the screen it was opened from
Assumes turbo-rails 2.0.x on Rails 8.1 and Hotwire Native 1.3 (built-in historical-location rules since 1.2.0); hotwire_native_app? keeps turbo_native_app? as an alias.