inertia-rails-setup
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseInertia Rails Setup
Inertia Rails 搭建指南
This skill helps you set up Inertia.js in a Ruby on Rails application with your choice of frontend framework.
本技能可帮助你在Ruby on Rails应用中搭配所选前端框架完成Inertia.js的搭建。
Recommended: Official Starter Kits
推荐方案:官方启动模板
For new projects, the fastest way to get started is cloning an official starter kit. These include authentication, shadcn/ui components, TypeScript, and optional SSR support out of the box.
对于新项目,最快的入门方式是克隆官方启动模板。这些模板默认包含身份验证、shadcn/ui组件、TypeScript,以及可选的SSR支持。
React Starter Kit (Recommended)
React启动模板(推荐)
bash
git clone https://github.com/inertia-rails/react-starter-kit myapp
cd myapp
bin/setupIncludes:
- React 19 + TypeScript
- shadcn/ui component library (20+ components)
- User authentication (login, register, password reset)
- Settings pages (profile, password, email, sessions, appearance)
- Multiple layouts (sidebar, header, auth variants)
- Dark mode support
- Kamal deployment config
- Optional SSR support
- Flash messages with Sonner toasts
bash
git clone https://github.com/inertia-rails/react-starter-kit myapp
cd myapp
bin/setup包含内容:
- React 19 + TypeScript
- shadcn/ui组件库(20+个组件)
- 用户身份验证(登录、注册、密码重置)
- 设置页面(个人资料、密码、邮箱、会话、外观)
- 多种布局(侧边栏、头部、身份验证变体)
- 深色模式支持
- Kamal部署配置
- 可选SSR支持
- 基于Sonner提示框的Flash消息
Vue Starter Kit
Vue启动模板
bash
git clone https://github.com/inertia-rails/vue-starter-kit myapp
cd myapp
bin/setupbash
git clone https://github.com/inertia-rails/vue-starter-kit myapp
cd myapp
bin/setupSvelte Starter Kit
Svelte启动模板
bash
git clone https://github.com/inertia-rails/svelte-starter-kit myapp
cd myapp
bin/setupbash
git clone https://github.com/inertia-rails/svelte-starter-kit myapp
cd myapp
bin/setupCustomizing the Starter Kit
自定义启动模板
After cloning:
-
Rename the app:bash
# Update config/application.rb module YourAppName class Application < Rails::Application -
Update database config:bash
# Edit config/database.yml with your settings -
Remove example pages you don't need:bash
# Delete from app/frontend/pages/ and corresponding controllers -
Add your own pages:bash
bin/rails generate controller Products index show # Create app/frontend/pages/products/index.tsx -
Customize the layout:
- Edit for main app
app/frontend/layouts/app-layout.tsx - Edit for navigation
app/frontend/components/nav-main.tsx
- Edit
克隆完成后:
-
重命名应用:bash
# 更新 config/application.rb module YourAppName class Application < Rails::Application -
更新数据库配置:bash
# 编辑 config/database.yml 配置你的数据库信息 -
移除不需要的示例页面:bash
# 删除 app/frontend/pages/ 下的文件及对应的控制器 -
添加自定义页面:bash
bin/rails generate controller Products index show # 创建 app/frontend/pages/products/index.tsx -
自定义布局:
- 编辑 配置主应用布局
app/frontend/layouts/app-layout.tsx - 编辑 配置导航栏
app/frontend/components/nav-main.tsx
- 编辑
Alternative: Generator Setup
替代方案:生成器搭建
If you prefer starting from scratch or adding Inertia to an existing Rails app:
如果你偏好从空白项目开始,或是为现有Rails应用添加Inertia支持:
Quick Setup
快速搭建
bash
undefinedbash
undefinedCreate new Rails app (if needed)
(如有需要)创建新的Rails应用
rails new myapp --skip-javascript
rails new myapp --skip-javascript
Add inertia_rails gem
添加 inertia_rails gem
bundle add inertia_rails
bundle add inertia_rails
Run the generator
运行生成器
bin/rails generate inertia:install
The generator will prompt you to select:
- Frontend framework (React, Vue, or Svelte)
- TypeScript support
- Tailwind CSS integrationbin/rails generate inertia:install
生成器会提示你选择:
- 前端框架(React、Vue或Svelte)
- TypeScript支持
- Tailwind CSS集成Manual Setup Steps
手动搭建步骤
1. Add the Gem
1. 添加Gem包
ruby
undefinedruby
undefinedGemfile
Gemfile
gem 'inertia_rails'
```bash
bundle installgem 'inertia_rails'
```bash
bundle install2. Install Vite Rails (if not present)
2. 安装Vite Rails(如未安装)
bash
bundle add vite_rails
bundle exec vite installbash
bundle add vite_rails
bundle exec vite install3. Configure the Root Layout
3. 配置根布局
Create or update :
app/views/layouts/application.html.erberb
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csp_meta_tag %>
<%= inertia_ssr_head %>
<%= vite_client_tag %>
<%= vite_javascript_tag 'application' %>
</head>
<body>
<%= yield %>
</body>
</html>创建或更新 :
app/views/layouts/application.html.erberb
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csp_meta_tag %>
<%= inertia_ssr_head %>
<%= vite_client_tag %>
<%= vite_javascript_tag 'application' %>
</head>
<body>
<%= yield %>
</body>
</html>4. Install Frontend Dependencies
4. 安装前端依赖
For React:
bash
npm install @inertiajs/react react react-domFor Vue 3:
bash
npm install @inertiajs/vue3 vueFor Svelte:
bash
npm install @inertiajs/svelte svelteReact环境:
bash
npm install @inertiajs/react react react-domVue 3环境:
bash
npm install @inertiajs/vue3 vueSvelte环境:
bash
npm install @inertiajs/svelte svelte5. Configure the Frontend Entry Point
5. 配置前端入口文件
Create :
app/frontend/entrypoints/application.jsReact:
javascript
import { createInertiaApp } from '@inertiajs/react'
import { createRoot } from 'react-dom/client'
createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.jsx', { eager: true })
return pages[`../pages/${name}.jsx`]
},
setup({ el, App, props }) {
createRoot(el).render(<App {...props} />)
},
})Vue 3:
javascript
import { createApp, h } from 'vue'
import { createInertiaApp } from '@inertiajs/vue3'
createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.vue', { eager: true })
return pages[`../pages/${name}.vue`]
},
setup({ el, App, props, plugin }) {
createApp({ render: () => h(App, props) })
.use(plugin)
.mount(el)
},
})Svelte:
javascript
import { createInertiaApp } from '@inertiajs/svelte'
createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.svelte', { eager: true })
return pages[`../pages/${name}.svelte`]
},
setup({ el, App }) {
new App({ target: el })
},
})创建 :
app/frontend/entrypoints/application.jsReact:
javascript
import { createInertiaApp } from '@inertiajs/react'
import { createRoot } from 'react-dom/client'
createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.jsx', { eager: true })
return pages[`../pages/${name}.jsx`]
},
setup({ el, App, props }) {
createRoot(el).render(<App {...props} />)
},
})Vue 3:
javascript
import { createApp, h } from 'vue'
import { createInertiaApp } from '@inertiajs/vue3'
createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.vue', { eager: true })
return pages[`../pages/${name}.vue`]
},
setup({ el, App, props, plugin }) {
createApp({ render: () => h(App, props) })
.use(plugin)
.mount(el)
},
})Svelte:
javascript
import { createInertiaApp } from '@inertiajs/svelte'
createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob('../pages/**/*.svelte', { eager: true })
return pages[`../pages/${name}.svelte`]
},
setup({ el, App }) {
new App({ target: el })
},
})6. Create the Pages Directory
6. 创建页面目录
bash
mkdir -p app/frontend/pagesbash
mkdir -p app/frontend/pages7. Configure the Initializer
7. 配置初始化器
Create :
config/initializers/inertia_rails.rbruby
undefined创建 :
config/initializers/inertia_rails.rbruby
undefinedfrozen_string_literal: true
frozen_string_literal: true
InertiaRails.configure do |config|
Asset versioning - triggers full reload when assets change
config.version = -> { ViteRuby.digest }
Flash keys exposed to frontend
config.flash_keys = %i[notice alert]
Deep merge shared data with page props
config.deep_merge_shared_data = true
Encrypt history for sensitive data (requires HTTPS)
config.encrypt_history = Rails.env.production?
end
undefinedInertiaRails.configure do |config|
资源版本控制 - 资源变更时触发全量重载
config.version = -> { ViteRuby.digest }
暴露给前端的Flash键
config.flash_keys = %i[notice alert]
深度合并共享数据与页面属性
config.deep_merge_shared_data = true
加密历史记录以保护敏感数据(需HTTPS)
config.encrypt_history = Rails.env.production?
end
undefined8. Set Up Shared Data
8. 设置共享数据
In :
app/controllers/application_controller.rbruby
class ApplicationController < ActionController::Base
inertia_share do
{
flash: {
notice: flash.notice,
alert: flash.alert
},
auth: {
user: current_user&.as_json(only: [:id, :name, :email])
}
}
end
end在 中:
app/controllers/application_controller.rbruby
class ApplicationController < ActionController::Base
inertia_share do
{
flash: {
notice: flash.notice,
alert: flash.alert
},
auth: {
user: current_user&.as_json(only: [:id, :name, :email])
}
}
end
end9. Create Your First Page
9. 创建你的第一个页面
Controller:
ruby
undefined控制器:
ruby
undefinedapp/controllers/home_controller.rb
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
render inertia: { message: 'Welcome to Inertia Rails!' }
end
end
**Route:**
```rubyclass HomeController < ApplicationController
def index
render inertia: { message: 'Welcome to Inertia Rails!' }
end
end
**路由:**
```rubyconfig/routes.rb
config/routes.rb
Rails.application.routes.draw do
root 'home#index'
end
**Page Component (React):**
```jsx
// app/frontend/pages/home/index.jsx
export default function Home({ message }) {
return (
<div>
<h1>{message}</h1>
</div>
)
}Page Component (Vue):
vue
<!-- app/frontend/pages/home/index.vue -->
<script setup>
defineProps(['message'])
</script>
<template>
<div>
<h1>{{ message }}</h1>
</div>
</template>Rails.application.routes.draw do
root 'home#index'
end
**页面组件(React):**
```jsx
// app/frontend/pages/home/index.jsx
export default function Home({ message }) {
return (
<div>
<h1>{message}</h1>
</div>
)
}页面组件(Vue):
vue
<!-- app/frontend/pages/home/index.vue -->
<script setup>
defineProps(['message'])
</script>
<template>
<div>
<h1>{{ message }}</h1>
</div>
</template>10. Start the Development Servers
10. 启动开发服务器
bash
undefinedbash
undefinedTerminal 1: Rails server
终端1:Rails服务器
bin/rails server
bin/rails server
Terminal 2: Vite dev server
终端2:Vite开发服务器
bin/vite dev
undefinedbin/vite dev
undefinedConfiguration Options Reference
配置选项参考
| Option | Default | Description |
|---|---|---|
| | Asset version for cache busting |
| | Default layout template |
| | Flash keys to share |
| | Deep merge props |
| | Encrypt browser history |
| | Enable SSR |
| | SSR server URL |
| | Auto-render Inertia |
| | Root element ID |
| 选项 | 默认值 | 说明 |
|---|---|---|
| | 资源版本号,用于缓存刷新 |
| | 默认布局模板 |
| | 要共享的Flash键 |
| | 深度合并属性 |
| | 加密浏览器历史记录 |
| | 启用SSR |
| | SSR服务器地址 |
| | 自动渲染Inertia页面 |
| | 根元素ID |
Environment Variables
环境变量
All config options can be set via prefixed env vars:
INERTIA_bash
INERTIA_SSR_ENABLED=true
INERTIA_ENCRYPT_HISTORY=true所有配置选项均可通过前缀为的环境变量设置:
INERTIA_bash
INERTIA_SSR_ENABLED=true
INERTIA_ENCRYPT_HISTORY=trueTroubleshooting
问题排查
"Cannot find module '@inertiajs/react'"
"Cannot find module '@inertiajs/react'"
Run to install dependencies.
npm install运行安装依赖包。
npm installBlank page with no errors
页面空白且无报错
Check browser console for JavaScript errors. Ensure Vite dev server is running.
检查浏览器控制台的JavaScript错误,确保Vite开发服务器正在运行。
Props not updating
属性未更新
Ensure you're using not .
render inertia:render json:确保你使用的是而非。
render inertia:render json:CSRF token errors
CSRF令牌错误
Inertia handles CSRF automatically via Axios. Ensure is enabled.
protect_from_forgeryInertia会通过Axios自动处理CSRF,确保已启用。
protect_from_forgery