Frontend
Server render HTML; Turbo làm cho nó có cảm giác như một SPA; Stimulus thêm các hành vi nhỏ; daisyUI cung cấp component. Replay so sánh các trang theo text hiển thị, nên hãy giữ nguyên mọi nội dung và mọi luồng; còn markup thì tùy bạn.
Thiết lập (front-door làm phần lớn việc này)
app/assets/tailwind/application.css:css@import "tailwindcss"; @plugin "./daisyui.mjs" { themes: light --default, dark --prefersdark; }daisyui.mjsvàdaisyui-theme.mjsnằm ngay cạnh nó (tải về từ các bản release của daisyUI; CLI standalone của Tailwind nạp chúng mà không cần Node). Chạybin/dev(hoặcbin/rails tailwindcss:watch) trong khi sửa view.- Theme:
<html data-theme="light">trong layout; bộ chuyển theme là một Stimulus controller setdocument.documentElement.dataset.themevà lưu nó vào cookie. - Layout:
navbarcủa daisyUI (hoặcdrawercho điều hướng bên cạnh) với các link của bản gốc,mainvớicontainer mx-auto p-4, flash message dùngalert alert-success/alert-error.
Ánh xạ
| Bản gốc | Rails |
|---|---|
| Component trang / route (React, Vue, Angular, Svelte, trang Next.js) | Controller action + app/views/<resource>/<action>.html.erb |
Component tái sử dụng (<ProductCard>) |
Partial _product_card.html.erb (hoặc một ViewComponent khi nó lặp lại kèm logic) |
| Props | Local của partial: render "product_card", product: p |
Link của client router (<Link>, router.push) |
link_to / button_to; Turbo Drive lo phần điều hướng kiểu SPA |
useState / ref cho state chỉ thuộc UI (open, tab, toggle) |
Stimulus controller với static values và targets |
Fetch rồi render (useEffect(fetch), SWR, React Query) |
Render trên server; các phần lazy đặt trong <turbo-frame src=... loading="lazy"> |
| Form submit phía client + JSON API | form_with model:; lỗi được render lại với status: :unprocessable_entity |
| Optimistic update / danh sách live | Response Turbo Stream (turbo_stream.append/replace/remove) |
| Cập nhật qua WebSocket / polling | turbo_stream_from @room + broadcasts_to trên model (Action Cable) |
| Modal | modal của daisyUI (<dialog>) được mở bởi một Stimulus controller, nội dung nằm trong turbo-frame id="modal" |
| Infinite scroll / "tải thêm" | Turbo Frame với loading="lazy" cho trang tiếp theo |
| Tìm kiếm khi gõ có debounce | Stimulus controller submit form sau 300 ms; kết quả nằm trong một Turbo Frame |
| Validation phía client (zod, yup) | Validation của ActiveModel, required/pattern của HTML5; class validator của daisyUI |
| CSS modules / styled-components / class của Bootstrap | Utility của Tailwind + component của daisyUI |
| Global store (Redux, Pinia, Context) | Server chính là store; session cho state theo từng người dùng; Stimulus outlet cho UI giữa các controller |
| Chuỗi i18n | config/locales/*.yml + t(".key") |
| Template phía server (Jinja, Thymeleaf, Blade, EJS, Razor, Go templates) | ERB: {{ x }} -> <%= x %>, {% for %} -> <% each do %>, block/section -> content_for / yield, include -> partial |
Các component daisyUI nên dùng
btn (btn-primary, btn-ghost, btn-sm), card (card-body, card-title, card-actions),
navbar, menu, drawer, table (table-zebra), badge, alert, modal, tabs, dropdown,
input/select/textarea với fieldset + label (daisyUI 5), join cho nhóm nút và
phân trang, stat cho dashboard, steps cho wizard, toast cho flash, skeleton và
loading cho các frame lazy.
Quy tắc
- Cùng URL, cùng tiêu đề trang (
content_for :title), cùng heading, label, text của nút, cột của bảng và trạng thái rỗng như bản gốc: replay kiểm tra phần text. - Form post tới cùng path với cùng tên field, trừ khi có ADR nói khác; giữ nguyên
thuộc tính
nameđể các case POST đã ghi vẫn chạy được. - Render phía server mọi thứ mà bản gốc hiển thị ở lần tải đầu; chỉ những gì vốn lazy trong bản gốc mới trở thành frame lazy.
- Accessibility: dùng
<button>/<a>thật,<label for>,aria-livetrên các target của Turbo Stream, chuyển focus vào modal. Các component daisyUI đều accessible khi bạn dùng đúng element. - Không dùng
<script>inline. Hành vi đặt trongapp/javascript/controllers/*_controller.js(bin/rails generate stimulus name), được pin bằng importmap. - System test (
bin/rails generate system_test) cho mỗi luồng có hành vi JavaScript.