Skip to content
inertia-coder logo

Inertia.js + Rails

inertia-coder

Build modern SPAs with Inertia.js and Rails using React, Vue, or Svelte. Use when creating Inertia pages, handling forms with useForm, managing shared props, or implementing client-side routing. Triggers on Inertia.js setup, SPA development, or React/Vue/Svelte with Rails.

SKILL.md

Full skill instructions

Inertia.js + Rails

Build modern single-page applications using Inertia.js with React/​Vue/​Svelte and Rails backend.

When to Use This Skill

  • Setting up Inertia.js with Rails
  • Creating Inertia page components
  • Handling forms with useForm hook
  • Managing shared props and flash messages
  • Client-side routing without API complexity
  • File uploads with progress tracking

What is Inertia.js?

Inertia.js allows you to build SPAs using classic server-side routing and controllers.

ApproachProsCons
Traditional Rails ViewsSimple, server-renderedLimited interactivity
Rails API + React SPAFull SPA experienceDuplicated routing, complex state
Inertia.jsSPA + server routingBest of both worlds

Quick Setup

# Gemfile
gem 'inertia_rails'
gem 'vite_rails'
bundle install
rails inertia:install  # Choose: React, Vue, or Svelte
# config/​initializers/​inertia_rails.rb
InertiaRails.configure do |config|
  config.version = ViteRuby.digest

  config.share do |controller|
    {
      auth: {
        user: controller.current_user&.as_json(only: [:id, :name, :email])
      },
      flash: controller.flash.to_hash
    }
  end
end

Controller Pattern

class ArticlesController < ApplicationController
  def index
    articles = Article.published.order(created_at: :desc)

    render inertia: 'Articles/​Index', props: {
      articles: articles.as_json(only: [:id, :title, :excerpt])
    }
  end

  def create
    @article = Article.new(article_params)

    if @article.save
      redirect_to article_path(@article), notice: 'Article created'
    else
      redirect_to new_article_path, inertia: { errors: @article.errors }
    end
  end
end

React Page Component

// app/​frontend/​pages/​Articles/​Index.jsx
import { Link } from '@inertiajs/​react'

export default function Index({ articles }) {
  return (
    <div>
      <h1>Articles</​h1>
      {articles.map(article => (
        <Link key={article.id} href={`/​articles/​${article.id}`}>
          <h2>{article.title}</​h2>
        </​Link>
      ))}
    </​div>
  )
}

Forms with useForm

import { useForm } from '@inertiajs/​react'

export default function New() {
  const { data, setData, post, processing, errors } = useForm({
    title: '',
    body: ''
  })

  function handleSubmit(e) {
    e.preventDefault()
    post('/​articles')
  }

  return (
    <form onSubmit={handleSubmit}>
      <input
        value={data.title}
        onChange={e => setData('title', e.target.value)}
      />
      {errors.title && <div className="error">{errors.title}</​div>}

      <textarea
        value={data.body}
        onChange={e => setData('body', e.target.value)}
      />
      {errors.body && <div className="error">{errors.body}</​div>}

      <button disabled={processing}>
        {processing ? 'Creating...' : 'Create'}
      </​button>
    </​form>
  )
}

Shared Layout

// app/​frontend/​layouts/​AppLayout.jsx
import { Link, usePage } from '@inertiajs/​react'

export default function AppLayout({ children }) {
  const { auth, flash } = usePage().props

  return (
    <div>
      <nav>
        <Link href="/">Home</​Link>
        {auth.user ? (
          <Link href="/​logout" method="delete">Logout</​Link>
        ) : (
          <Link href="/​login">Login</​Link>
        )}
      </​nav>

      {flash.success && <div className="alert-success">{flash.success}</​div>}

      <main>{children}</​main>
    </​div>
  )
}

// Assign layout to page
Index.layout = page => <AppLayout>{page}</​AppLayout>

File Upload

import { useForm } from '@inertiajs/​react'

const { data, setData, post, progress } = useForm({
  avatar: null
})

<input
  type="file"
  onChange={e => setData('avatar', e.target.files[0])}
/>

{progress && <progress value={progress.percentage} max="100" />}

<button onClick={() => post('/​profile/​avatar', { forceFormData: true })}>
  Upload
</​button>

Best Practices

DO

  • Use <Link> instead of <a> tags
  • Share common data via config (auth, flash)
  • Validate on server - client is UX only
  • Show loading states with processing
  • Use layouts for consistent navigation

DON'T

  • Don't use window.location - breaks SPA
  • Don't create REST APIs - Inertia doesn't need them
  • Don't fetch data client-side - server provides props
  • Don't bypass Inertia router - breaks behavior

Detailed References

For framework-specific patterns:

  • references/​react-patterns.md - React hooks, TypeScript, advanced patterns
  • references/​vue-patterns.md - Vue 3 Composition API patterns
  • references/​svelte-patterns.md - Svelte stores and reactivity patterns

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View