inertia-rails-best-practices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Inertia Rails Best Practices

Inertia Rails 最佳实践

A comprehensive guide to building high-quality Inertia.js applications with Ruby on Rails. This skill provides 50+ rules across 8 categories, prioritized by impact on application quality, performance, and maintainability.
这是一份使用Ruby on Rails构建高质量Inertia.js应用的综合指南。本技能涵盖8大分类下的50+条规则,根据对应用质量、性能和可维护性的影响程度进行优先级排序。

When to Use This Skill

适用场景

  • Writing new Inertia Rails controllers or pages
  • Reviewing existing Inertia Rails code
  • Refactoring Rails applications to use Inertia
  • Optimizing performance of Inertia applications
  • Implementing authentication/authorization patterns
  • Building forms with validation
  • Setting up testing for Inertia responses
  • 编写新的Inertia Rails控制器或页面
  • 评审现有Inertia Rails代码
  • 重构Rails应用以接入Inertia
  • 优化Inertia应用的性能
  • 实现认证/授权模式
  • 构建带验证的表单
  • 为Inertia响应设置测试

Categories Overview

分类概览

#CategoryPriorityImpact
1Server-Side Setup & ConfigurationCRITICALFoundation for all Inertia functionality
2Props & Data ManagementCRITICAL2-5× performance improvement, security
3Forms & ValidationHIGHUser experience, data integrity
4Navigation & RoutingHIGHSPA experience, performance
5Performance OptimizationMEDIUM-HIGH30-70% faster page loads
6SecurityMEDIUM-HIGHProtection against common vulnerabilities
7TestingMEDIUMCode quality, maintainability
8Advanced PatternsMEDIUMScalability, complex use cases
#分类优先级影响
1服务端配置与设置关键所有Inertia功能的基础
2Props与数据管理关键提升2-5倍性能,保障安全
3表单与验证用户体验、数据完整性
4导航与路由SPA体验、性能
5性能优化中高页面加载速度提升30-70%
6安全中高防范常见漏洞
7测试代码质量、可维护性
8高级模式可扩展性、复杂场景

Quick Reference

快速参考

Server-Side Setup & Configuration (CRITICAL)

服务端配置与设置(关键)

IDRuleContext
setup-01Use the Rails generator for initial setupNew projects
setup-02Configure asset versioning for cache bustingAll projects
setup-03Set up proper layout inheritanceMulti-layout apps
setup-04Configure flash keys appropriatelyFlash messaging
setup-05Use environment variables for configurationDeployment
setup-06Set up default render behavior thoughtfullyConvention over configuration
ID规则适用场景
setup-01使用Rails生成器完成初始配置新项目
setup-02配置资源版本控制以实现缓存击穿所有项目
setup-03配置正确的布局继承多布局应用
setup-04合理配置flash键消息提示
setup-05使用环境变量进行配置部署场景
setup-06谨慎设置默认渲染行为约定优于配置

Props & Data Management (CRITICAL)

Props与数据管理(关键)

IDRuleContext
props-01Return only necessary data in propsAll responses
props-02Use shared data for global propsAuth, settings
props-03Leverage lazy evaluation with lambdasExpensive operations
props-04Use deferred props for non-critical dataPerformance
props-05Implement partial reloads correctlyData refreshing
props-06Never expose sensitive data in propsSecurity
props-07Use proper serialization with as_jsonData formatting
props-08Implement deep merge when appropriateNested data
props-09Use once props for stable dataNavigation performance
ID规则适用场景
props-01仅在Props中返回必要数据所有响应
props-02使用共享数据传递全局Props认证、设置
props-03结合lambda实现延迟求值高开销操作
props-04为非关键数据使用延迟Props性能优化
props-05正确实现部分重载数据刷新
props-06绝不在Props中暴露敏感数据安全
props-07使用as_json进行正确序列化数据格式化
props-08适时实现深度合并嵌套数据
props-09为稳定数据使用一次性Props导航性能

Forms & Validation (HIGH)

表单与验证(高)

IDRuleContext
forms-01Use useForm helper for complex formsProgrammatic control
forms-02Use Form component for simple formsDeclarative forms
forms-03Handle validation errors properlyUser feedback
forms-04Implement error bags for multiple formsMulti-form pages
forms-05Use redirect pattern after form submissionPRG pattern
forms-06Handle file uploads correctlyMultipart forms
forms-07Preserve form state on validation errorsUser experience
forms-08Use dotted notation for nested dataComplex forms
ID规则适用场景
forms-01为复杂表单使用useForm助手程序化控制
forms-02为简单表单使用Form组件声明式表单
forms-03正确处理验证错误用户反馈
forms-04为多表单页面实现错误袋多表单页面
forms-05表单提交后使用重定向模式PRG模式
forms-06正确处理文件上传多部分表单
forms-07验证错误时保留表单状态用户体验
forms-08为嵌套数据使用点记法复杂表单

Navigation & Routing (HIGH)

导航与路由(高)

IDRuleContext
nav-01Use Link component for internal navigationSPA behavior
nav-02Use inertia_location for external redirectsExternal URLs
nav-03Implement preserve-scroll appropriatelyScroll position
nav-04Use preserve-state for component stateForm preservation
nav-05Configure proper HTTP methods on linksRESTful actions
nav-06Use the inertia route helper for static pagesSimple routes
nav-07Handle 303 redirects correctlyPOST/PUT/PATCH/DELETE
ID规则适用场景
nav-01使用Link组件进行内部导航SPA行为
nav-02使用inertia_location进行外部重定向外部URL
nav-03合理实现滚动位置保留滚动位置
nav-04使用preserve-state保留组件状态表单状态保留
nav-05为链接配置正确的HTTP方法RESTful操作
nav-06为静态页面使用inertia路由助手简单路由
nav-07正确处理303重定向POST/PUT/PATCH/DELETE请求

Performance Optimization (MEDIUM-HIGH)

性能优化(中高)

IDRuleContext
perf-01Implement code splitting with dynamic importsBundle size
perf-02Use prefetching for likely navigationPerceived performance
perf-03Configure stale-while-revalidate cachingData freshness
perf-04Use polling only when necessaryReal-time updates
perf-05Implement infinite scrolling with merge propsLarge datasets
perf-06Optimize progress indicatorsUser feedback
perf-07Use async visits for non-blocking operationsBackground tasks
ID规则适用场景
perf-01结合动态导入实现代码分割包体积优化
perf-02为可能的导航使用预加载感知性能提升
perf-03配置stale-while-revalidate缓存数据新鲜度
perf-04仅在必要时使用轮询实时更新
perf-05结合合并Props实现无限滚动大数据集
perf-06优化进度指示器用户反馈
perf-07为非阻塞操作使用异步访问后台任务

Security (MEDIUM-HIGH)

安全(中高)

IDRuleContext
sec-01Implement authentication server-sideAuth patterns
sec-02Pass authorization results as propsPermission checks
sec-03Use history encryption for sensitive dataBrowser history
sec-04Rely on Rails CSRF protectionForm security
sec-05Validate and sanitize all input server-sideData validation
sec-06Use strong parameters in controllersMass assignment
ID规则适用场景
sec-01在服务端实现认证认证模式
sec-02将授权结果作为Props传递权限校验
sec-03为敏感数据使用历史记录加密浏览器历史
sec-04依赖Rails的CSRF保护表单安全
sec-05在服务端验证并清洗所有输入数据验证
sec-06在控制器中使用强参数批量赋值防护

Testing (MEDIUM)

测试(中)

IDRuleContext
test-01Use RSpec matchers for Inertia responsesRSpec testing
test-02Use Minitest assertions for InertiaMinitest testing
test-03Test partial reloads and deferred propsAdvanced features
test-04Implement end-to-end tests with CapybaraIntegration testing
test-05Test flash messages after redirectsFlash testing
test-06Verify component renderingResponse validation
ID规则适用场景
test-01使用RSpec匹配器测试Inertia响应RSpec测试
test-02使用Minitest断言测试InertiaMinitest测试
test-03测试部分重载和延迟Props高级功能
test-04使用Capybara实现端到端测试集成测试
test-05测试重定向后的flash消息消息提示测试
test-06验证组件渲染响应校验

Advanced Patterns (MEDIUM)

高级模式(中)

IDRuleContext
adv-01Implement persistent layoutsState preservation
adv-02Use custom component path resolversNon-standard paths
adv-03Configure prop transformersData transformation
adv-04Handle SSR appropriatelySEO requirements
adv-05Implement view transitionsModern animations
adv-06Use scroll regions for complex layoutsScroll management
adv-07Handle events system effectivelyLifecycle hooks

For detailed explanations, code examples, and implementation guidance for each rule, see AGENTS.md.
ID规则适用场景
adv-01实现持久化布局状态保留
adv-02使用自定义组件路径解析器非标准路径
adv-03配置Props转换器数据转换
adv-04正确处理SSRSEO需求
adv-05实现视图过渡现代动画
adv-06为复杂布局使用滚动区域滚动管理
adv-07高效处理事件系统生命周期钩子

如需每条规则的详细说明、代码示例及实现指导,请查看AGENTS.md