Skip to content

前端

由伺服器渲染 HTML;Turbo 讓它用起來像 SPA;Stimulus 補上小型互動行為;daisyUI 提供元件。重播會以可見文字比對頁面,所以每一段內容、每一個流程都要保留;標記則由你決定。

設定(front-door 會幫你做大部分)

  • app/assets/tailwind/application.css:
    css
    @import "tailwindcss";
    @plugin "./daisyui.mjs" { themes: light --default, dark --prefersdark; }
    
    daisyui.mjs 與 daisyui-theme.mjs 放在它旁邊(從 daisyUI 的 releases 下載;Tailwind 的 standalone CLI 不需要 Node 就能載入它們)。編輯 view 時請執行 bin/dev(或 bin/rails tailwindcss:watch)。
  • 主題:在 layout 中使用 <html data-theme="light">;主題切換器是一個 Stimulus controller,負責設定 document.documentElement.dataset.theme 並存進 cookie。
  • Layout:使用 daisyUI 的 navbar(側邊導覽則用 drawer)放上原版的連結,main 使用 container mx-auto p-4,flash 訊息使用 alert alert-success / alert-error。

對應方式

原版 Rails
頁面 / 路由元件(React、Vue、Angular、Svelte、Next.js 頁面) Controller action + app/views/<resource>/<action>.html.erb
可重複使用的元件(<ProductCard>) Partial _product_card.html.erb(當它重複出現且帶有邏輯時,改用 ViewComponent)
Props Partial 的區域變數:render "product_card", product: p
用戶端路由連結(<Link>、router.push) link_to / button_to;由 Turbo Drive 負責 SPA 式導覽
只用於 UI 狀態的 useState / ref(展開、分頁、切換) 使用 static values 與 targets 的 Stimulus controller
先 fetch 再渲染(useEffect(fetch)、SWR、React Query) 在伺服器端渲染;延遲載入的部分放進 <turbo-frame src=... loading="lazy">
用戶端送出的表單 + JSON API form_with model:;錯誤時以 status: :unprocessable_entity 重新渲染
樂觀更新 / 即時清單 Turbo Stream 回應(turbo_stream.append/replace/remove)
WebSocket / 輪詢更新 turbo_stream_from @room + 在 model 上使用 broadcasts_to(Action Cable)
Modal 由 Stimulus controller 開啟的 daisyUI modal(<dialog>),內容放在 turbo-frame id="modal" 中
無限捲動 / 「載入更多」 下一頁使用帶 loading="lazy" 的 Turbo Frame
有 debounce 的即打即搜 300 ms 後送出表單的 Stimulus controller;結果放在 Turbo Frame 中
用戶端驗證(zod、yup) ActiveModel 驗證、HTML5 required/pattern;daisyUI validator class
CSS modules / styled-components / Bootstrap class Tailwind utility + daisyUI 元件
全域 store(Redux、Pinia、Context) 伺服器就是 store;每位使用者的狀態放在 session;跨 controller 的 UI 使用 Stimulus outlets
i18n 字串 config/locales/*.yml + t(".key")
伺服器端模板(Jinja、Thymeleaf、Blade、EJS、Razor、Go templates) ERB:{{ x }} -> <%= x %>,{% for %} -> <% each do %>,blocks/sections -> content_for / yield,includes -> partial

常用的 daisyUI 元件

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、搭配 fieldset + label 的 input/select/textarea(daisyUI 5)、用於按鈕群組與分頁的 join、用於儀表板的 stat、用於精靈流程的 steps、用於 flash 的 toast,以及用於延遲載入 frame 的 skeleton 與 loading。

規則

  • URL、頁面標題(content_for :title)、標題、標籤、按鈕文字、表格欄位與空狀態都要和原版相同:重播會檢查文字。
  • 除非 ADR 另有規定,表單要送到相同的路徑並使用相同的欄位名稱;保留 name 屬性,讓錄製的 POST 案例仍然有效。
  • 原版首次載入時顯示的所有內容都要在伺服器端渲染;只有原版中原本就延遲載入的部分,才改成延遲載入的 frame。
  • 無障礙:使用真正的 <button>/<a>、<label for>,在 Turbo Stream 目標上加 aria-live,並把焦點移進 modal。只要使用正確的元素,daisyUI 元件就具備無障礙性。
  • 不要使用 inline <script>。互動行為放在 app/javascript/controllers/*_controller.js(bin/rails generate stimulus name),並由 importmap pin 住。
  • 每個帶有 JavaScript 行為的流程都要有系統測試(bin/rails generate system_test)。
在 GitHub 上編輯此頁 →