inertia-rails-setup
Original:🇺🇸 English
Translated
Set up a new Inertia Rails project or add Inertia to an existing Rails application. Use when creating new projects, configuring Inertia, or setting up the development environment with React, Vue, or Svelte.
12installs
Added on
NPX Install
npx skill4agent add cole-robertson/inertia-rails-skills inertia-rails-setupTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Inertia Rails Setup
This skill helps you set up Inertia.js in a Ruby on Rails application with your choice of frontend framework.
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.
React Starter Kit (Recommended)
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
Vue Starter Kit
bash
git clone https://github.com/inertia-rails/vue-starter-kit myapp
cd myapp
bin/setupSvelte Starter Kit
bash
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
Alternative: Generator Setup
If you prefer starting from scratch or adding Inertia to an existing Rails app:
Quick Setup
bash
# Create new Rails app (if needed)
rails new myapp --skip-javascript
# Add inertia_rails gem
bundle add inertia_rails
# Run the generator
bin/rails generate inertia:installThe generator will prompt you to select:
- Frontend framework (React, Vue, or Svelte)
- TypeScript support
- Tailwind CSS integration
Manual Setup Steps
1. Add the Gem
ruby
# Gemfile
gem 'inertia_rails'bash
bundle install2. Install Vite Rails (if not present)
bash
bundle add vite_rails
bundle exec vite install3. Configure the Root Layout
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>4. Install Frontend Dependencies
For React:
bash
npm install @inertiajs/react react react-domFor Vue 3:
bash
npm install @inertiajs/vue3 vueFor Svelte:
bash
npm install @inertiajs/svelte svelte5. Configure the Frontend Entry Point
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 })
},
})6. Create the Pages Directory
bash
mkdir -p app/frontend/pages7. Configure the Initializer
Create :
config/initializers/inertia_rails.rbruby
# 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?
end8. Set Up Shared Data
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
end9. Create Your First Page
Controller:
ruby
# app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
render inertia: { message: 'Welcome to Inertia Rails!' }
end
endRoute:
ruby
# config/routes.rb
Rails.application.routes.draw do
root 'home#index'
endPage 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>10. Start the Development Servers
bash
# Terminal 1: Rails server
bin/rails server
# Terminal 2: Vite dev server
bin/vite devConfiguration 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 |
Environment Variables
All config options can be set via prefixed env vars:
INERTIA_bash
INERTIA_SSR_ENABLED=true
INERTIA_ENCRYPT_HISTORY=trueTroubleshooting
"Cannot find module '@inertiajs/react'"
Run to install dependencies.
npm installBlank page with no errors
Check browser console for JavaScript errors. Ensure Vite dev server is running.
Props not updating
Ensure you're using not .
render inertia:render json:CSRF token errors
Inertia handles CSRF automatically via Axios. Ensure is enabled.
protect_from_forgery