Skip to content

Use Turbo Streams for token create/destroy and add system tests#41

Merged
HamptonMakes merged 1 commit intomainfrom
hampton/turbo-stream-tokens
Mar 11, 2026
Merged

Use Turbo Streams for token create/destroy and add system tests#41
HamptonMakes merged 1 commit intomainfrom
hampton/turbo-stream-tokens

Conversation

@HamptonMakes
Copy link
Collaborator

@HamptonMakes HamptonMakes commented Mar 10, 2026

Problem

The tokens page form submission was broken with Turbo Drive. After creating a token, Turbo rejected the response with Form responses must redirect to another location because the redirect target matched the form action URL. The token reveal (with the raw token value) never appeared — the form stayed stuck in a loading state.

Fix

Respond with Turbo Streams for both create and destroy actions instead of redirecting:

  • create: updates the token reveal section, resets the form, prepends the new token row
  • destroy: replaces the row in-place showing revoked state

Extracted shared partials (_token_row, _token_reveal, _form) — zero HTML duplication between the index view and turbo_stream templates. HTML fallback preserved for non-Turbo clients.

Also removed the empty-state conditional so the table always renders — this ensures the turbo_stream prepend works even when creating the first token.

System tests

Added Capybara + headless Chrome system tests covering:

  • Token creation with raw value display (verifies 64-char hex token appears)
  • Empty state (first token creation)
  • Token revocation via Turbo Stream (badge update, button removal)

Updated existing request specs to match redirect/auth behavior.

Test results

All 389 specs passing.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b49bcfd3ce

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

<%= render "form" %>
<% end %>

<%= turbo_stream.prepend "tokens-list" do %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Replace empty-state card instead of prepending missing target

When a user creates their first token, this stream prepends into tokens-list, but index.html.erb only renders <tbody id="tokens-list"> when @api_tokens.any? is true. In the empty-state path there is no target element, so Turbo ignores the prepend and the new token row does not appear until a full reload, which makes the create flow look broken for new users.

Useful? React with 👍 / 👎.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! Removed the empty-state conditional entirely — the table always renders (with an empty tbody). The turbo_stream prepend into #tokens-list now works regardless of whether it's the first token or not.

Also added a dedicated system test for the empty-state flow to keep this covered.

The tokens controller was returning a redirect after create, which Turbo
Drive rejected with 'Form responses must redirect to another location'
because the redirect target matched the form action URL. The token reveal
section and flash never appeared — the form just stayed stuck in a busy state.

Fix by responding with Turbo Streams for both create and destroy actions:
- create: updates the token reveal, resets the form, prepends the new row
- destroy: replaces the row in-place with revoked state

Extract shared partials (_token_row, _token_reveal, _form) so there is
zero HTML duplication between the index view and turbo_stream templates.

Add system tests covering both create and revoke flows to catch Turbo
integration issues that request specs miss. Update existing request specs
to match the new redirect behavior.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019cd872-3abe-7556-8927-9966fe044b6c
@HamptonMakes HamptonMakes force-pushed the hampton/turbo-stream-tokens branch from b49bcfd to 420472e Compare March 11, 2026 14:09
@HamptonMakes
Copy link
Collaborator Author

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. 🚀

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@HamptonMakes HamptonMakes merged commit d5f5449 into main Mar 11, 2026
4 checks passed
@HamptonMakes HamptonMakes deleted the hampton/turbo-stream-tokens branch March 11, 2026 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant