inertia-rails
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese<essential_principles>
<essential_principles>
How Inertia Rails Works
Inertia Rails 工作原理
Inertia is a bridge between Rails and modern JavaScript frameworks (React, Vue, Svelte). It lets you build SPAs using classic server-side routing and controllers—no client-side routing or separate APIs needed.
Inertia 是Rails与现代JavaScript框架(React、Vue、Svelte)之间的桥梁。它允许你使用经典的服务器端路由和控制器来构建单页应用(SPA)——无需客户端路由或独立的API。
1. Server-Driven Architecture
1. 服务器驱动架构
Controllers render Inertia responses instead of views. Data flows from Rails to JavaScript components as props:
ruby
render inertia: 'Users/Show', props: { user: user.as_json }控制器渲染Inertia响应而非视图。数据以props的形式从Rails流向JavaScript组件:
ruby
render inertia: 'Users/Show', props: { user: user.as_json }2. No Client-Side Routing
2. 无客户端路由
Routes live in . Inertia intercepts link clicks and form submissions, making XHR requests that return JSON with component name and props. The client swaps components without full page reloads.
config/routes.rb路由定义在中。Inertia会拦截链接点击和表单提交,发起XHR请求,返回包含组件名称和props的JSON数据。客户端无需重新加载整个页面即可切换组件。
config/routes.rb3. Convention-Based Component Resolution
3. 基于约定的组件解析
By default, in renders . Override with explicit component names when needed.
render inertia: { user: }UsersController#showapp/frontend/pages/users/show.(jsx|vue|svelte)默认情况下,中的会渲染组件。必要时可通过显式指定组件名称来覆盖默认规则。
UsersController#showrender inertia: { user: }app/frontend/pages/users/show.(jsx|vue|svelte)4. Validation via Redirects
4. 通过重定向实现验证
Unlike typical SPAs returning 422 JSON responses, Inertia follows traditional Rails patterns:
- Controller validates and redirects back on failure
- Errors are flashed and shared as props
- Form state is preserved automatically
与典型SPA返回422 JSON响应不同,Inertia遵循传统Rails模式:
- 控制器验证失败时重定向回原页面
- 错误信息会被闪存并作为props共享
- 表单状态会自动保留
5. Shared Data Pattern
5. 共享数据模式
Use in controllers to provide data to all pages (current_user, flash messages, notifications). Data is included in every response—use sparingly.
</essential_principles>
<intake>
**What would you like to do?**
inertia_share- Set up Inertia in a Rails project
- Create a new page/component
- Build a form with validation
- Add shared data (auth, flash, etc.)
- Handle redirects and navigation
- Debug an Inertia issue
- Test an Inertia controller
- Optimize with partial reloads
- Something else
Wait for response before proceeding.
</intake>
<routing>
| Response | Workflow |
|----------|----------|
| 1, "setup", "install", "start", "new" | `workflows/setup-inertia.md` |
| 2, "page", "component", "create", "render" | `workflows/create-page.md` |
| 3, "form", "validation", "submit", "useForm" | `workflows/build-form.md` |
| 4, "shared", "auth", "flash", "current_user" | `workflows/shared-data.md` |
| 5, "redirect", "navigate", "link", "router" | `workflows/navigation.md` |
| 6, "debug", "fix", "error", "not working" | `workflows/debug-inertia.md` |
| 7, "test", "spec", "minitest", "rspec" | `workflows/testing.md` |
| 8, "partial", "reload", "optimize", "only" | `workflows/partial-reloads.md` |
| 9, other | Clarify, then select workflow or references |
After reading the workflow, follow it exactly.
</routing>
<verification_loop>
在控制器中使用可为所有页面提供数据(如current_user、闪存消息、通知)。这些数据会包含在每个响应中——请谨慎使用。
</essential_principles>
<intake>
**你想要进行什么操作?**
inertia_share- 在Rails项目中搭建Inertia
- 创建新页面/组件
- 构建带验证的表单
- 添加共享数据(身份验证、闪存等)
- 处理重定向与导航
- 调试Inertia问题
- 测试Inertia控制器
- 用部分加载优化性能
- 其他操作
请先回复,再继续下一步。
</intake>
<routing>
| 响应内容 | 工作流 |
|----------|----------|
| 1, "setup", "install", "start", "new" | `workflows/setup-inertia.md` |
| 2, "page", "component", "create", "render" | `workflows/create-page.md` |
| 3, "form", "validation", "submit", "useForm" | `workflows/build-form.md` |
| 4, "shared", "auth", "flash", "current_user" | `workflows/shared-data.md` |
| 5, "redirect", "navigate", "link", "router" | `workflows/navigation.md` |
| 6, "debug", "fix", "error", "not working" | `workflows/debug-inertia.md` |
| 7, "test", "spec", "minitest", "rspec" | `workflows/testing.md` |
| 8, "partial", "reload", "optimize", "only" | `workflows/partial-reloads.md` |
| 9, other | 先明确需求,再选择对应工作流或参考文档 |
阅读工作流后,请严格按照其步骤操作。
</routing>
<verification_loop>
After Every Change
每次修改后需检查
- Does the page render? Check Rails logs for Inertia response
- Are props received? Console.log props in component
- Does navigation work? Click links - should not full-reload
- Do forms submit? Check Network tab for XHR requests
- Are errors displayed? Test validation failures
ruby
undefined- 页面是否正常渲染? 查看Rails日志中的Inertia响应
- 是否接收到props? 在组件中用console.log打印props
- 导航是否正常工作? 点击链接——不应触发整页刷新
- 表单是否能提交? 在网络标签页查看XHR请求
- 错误信息是否显示? 测试验证失败场景
ruby
undefinedRails debug - check response type
Rails调试 - 检查响应类型
render inertia: 'Page', props: { debug: true }
```javascript
// Frontend debug - log all props
console.log(usePage().props)Report to the user:
- "Inertia response: ✓"
- "Props received: X keys"
- "Navigation: SPA mode ✓/✗"
- "Ready for testing" </verification_loop>
<reference_index>
render inertia: 'Page', props: { debug: true }
```javascript
// 前端调试 - 打印所有props
console.log(usePage().props)向用户反馈:
- "Inertia响应: ✓"
- "接收到的props数量: X个"
- "导航: SPA模式 ✓/✗"
- "已准备好测试" </verification_loop>
<reference_index>
Domain Knowledge
领域知识
All in :
references/Core: setup.md, responses.md, forms.md, validation.md
Data Flow: shared-data.md, links.md
Quality: testing.md
Cookbook: cookbook.md (shadcn/ui, Inertia Modal, meta tags, error types)
</reference_index>
<workflows_index>
所有内容位于目录下:
references/核心内容: setup.md, responses.md, forms.md, validation.md
数据流: shared-data.md, links.md
质量保障: testing.md
实用指南: cookbook.md(shadcn/ui、Inertia模态框、元标签、错误类型)
</reference_index>
<workflows_index>
Workflows
工作流
All in :
workflows/| File | Purpose |
|---|---|
| setup-inertia.md | Install and configure Inertia Rails |
| create-page.md | Build new pages with props |
| build-form.md | Forms with validation and useForm |
| shared-data.md | Share auth/flash across all pages |
| navigation.md | Links, redirects, router methods |
| debug-inertia.md | Find and fix Inertia issues |
| testing.md | Test Inertia controllers |
| partial-reloads.md | Optimize with selective data loading |
| </workflows_index> |
所有内容位于目录下:
workflows/| 文件 | 用途 |
|---|---|
| setup-inertia.md | 安装并配置Inertia Rails |
| create-page.md | 构建带props的新页面 |
| build-form.md | 构建带验证的表单及useForm使用 |
| shared-data.md | 在所有页面共享身份验证/闪存数据 |
| navigation.md | 链接、重定向、路由方法 |
| debug-inertia.md | 排查并修复Inertia问题 |
| testing.md | 测试Inertia控制器 |
| partial-reloads.md | 用选择性数据加载优化性能 |
| </workflows_index> |