Loading...
Loading...
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.
npx skill4agent add cole-robertson/inertia-rails-skills inertia-rails-setupgit clone https://github.com/inertia-rails/react-starter-kit myapp
cd myapp
bin/setupgit clone https://github.com/inertia-rails/vue-starter-kit myapp
cd myapp
bin/setupgit clone https://github.com/inertia-rails/svelte-starter-kit myapp
cd myapp
bin/setup# Update config/application.rb
module YourAppName
class Application < Rails::Application# Edit config/database.yml with your settings# Delete from app/frontend/pages/ and corresponding controllersbin/rails generate controller Products index show
# Create app/frontend/pages/products/index.tsxapp/frontend/layouts/app-layout.tsxapp/frontend/components/nav-main.tsx# 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:install# Gemfile
gem 'inertia_rails'bundle installbundle add vite_rails
bundle exec vite installapp/views/layouts/application.html.erb<!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>npm install @inertiajs/react react react-domnpm install @inertiajs/vue3 vuenpm install @inertiajs/svelte svelteapp/frontend/entrypoints/application.jsimport { 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} />)
},
})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)
},
})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 })
},
})mkdir -p app/frontend/pagesconfig/initializers/inertia_rails.rb# 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?
endapp/controllers/application_controller.rbclass 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/home_controller.rb
class HomeController < ApplicationController
def index
render inertia: { message: 'Welcome to Inertia Rails!' }
end
end# config/routes.rb
Rails.application.routes.draw do
root 'home#index'
end// app/frontend/pages/home/index.jsx
export default function Home({ message }) {
return (
<div>
<h1>{message}</h1>
</div>
)
}<!-- app/frontend/pages/home/index.vue -->
<script setup>
defineProps(['message'])
</script>
<template>
<div>
<h1>{{ message }}</h1>
</div>
</template># Terminal 1: Rails server
bin/rails server
# Terminal 2: Vite dev server
bin/vite dev| 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 |
INERTIA_INERTIA_SSR_ENABLED=true
INERTIA_ENCRYPT_HISTORY=truenpm installrender inertia:render json:protect_from_forgery