forked from fixin.me/fixin.me
Replace fetch() calls with Turbo form submission via requestSubmit()
setDefaultUnit and drop previously made raw fetch() requests and called Turbo.renderStreamMessage() manually. Now both create a temporary <form>, append hidden inputs, and call form.requestSubmit() — Turbo intercepts the submission natively, handling CSRF, stream responses, and lifecycle. Also document this convention in CLAUDE.md. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
22
CLAUDE.md
22
CLAUDE.md
@@ -77,6 +77,28 @@ default/ namespace for default units import/export and admin panel
|
||||
root → /units (authenticated), /sign_in (unauthenticated)
|
||||
```
|
||||
|
||||
## JavaScript Conventions
|
||||
|
||||
### No manual fetch() — use Turbo
|
||||
|
||||
Never make AJAX requests with `fetch()` in JavaScript. Use Turbo's built-in mechanisms instead:
|
||||
|
||||
- **Links/buttons that trigger server actions**: use `data: {turbo_stream: true}` on the element (link or button_to form).
|
||||
- **Dynamic form submissions from JS** (where HTML alone isn't enough): create a form element, append hidden inputs, and call `form.requestSubmit()`. Turbo intercepts it automatically — no manual CSRF handling, no `Turbo.renderStreamMessage()`.
|
||||
```javascript
|
||||
var form = document.createElement('form');
|
||||
form.action = url; form.method = 'post'; form.dataset.turboStream = 'true';
|
||||
// append hidden inputs...
|
||||
form.addEventListener('turbo:submit-end', function() { form.remove(); });
|
||||
document.body.appendChild(form);
|
||||
form.requestSubmit();
|
||||
```
|
||||
- **Server-rendered HTML**: use ERB partials and Turbo Stream views (`*.turbo_stream.erb`), never build HTML in JavaScript.
|
||||
|
||||
### No HTML generation in JavaScript
|
||||
|
||||
Never use JavaScript to build and insert HTML (no `innerHTML =`, no `createElement` trees for content). Render HTML server-side in ERB partials; update the DOM via Turbo Stream actions (`replace`, `update`, `append`, etc.).
|
||||
|
||||
## Database Requirements
|
||||
|
||||
The database must support:
|
||||
|
||||
Reference in New Issue
Block a user