重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
alba-inertia by inertia-rails/skills
npx skills add https://github.com/inertia-rails/skills --skill alba-inertia要求:在 Gemfile 中包含 alba、typelizer、alba-inertia 这些 gems。
用于 Inertia 属性的 Alba 序列化器,带有自动生成的 TypeScript 类型。用结构化、类型安全的资源替换 as_json(only: [...])。
创建资源前,请思考:
UserResource) —— 跨页面共享UsersIndexResource) —— 每个控制器动作一个SharedPropsResource)绝对不要:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
as_json —— 这会绕过类型生成并创建无类型的属性typelize_from —— Typelizer 无法推断列类型并会生成 unknowndeclare module 增强放在 serializers/index.ts 中 —— Typelizer 生成的类型应放在 serializers/index.ts 中,手动的 InertiaConfig 应放在 globals.d.ts 中# app/resources/application_resource.rb
class ApplicationResource
include Alba::Resource
helper Typelizer::DSL # 启用 typelize, typelize_from
helper Alba::Inertia::Resource # 启用 attributes 上的 inertia: 选项
include Rails.application.routes.url_helpers
end
# app/controllers/inertia_controller.rb
class InertiaController < ApplicationController
include Alba::Inertia::Controller
inertia_share { SharedPropsResource.new(self).to_inertia }
end
# app/resources/user_resource.rb
class UserResource < ApplicationResource
typelize_from User # 当资源名称与模型不匹配时需要
attributes :id, :name, :email
typelize :string?
attribute :avatar_url do |user|
user.avatar.attached? ? rails_blob_path(user.avatar, only_path: true) : nil
end
end
# app/resources/users/index_resource.rb
# 命名约定:{Controller}{Action}Resource
class UsersIndexResource < ApplicationResource
has_many :users, resource: UserResource
typelize :string
attribute :search do |obj, _|
obj.params.dig(:filters, :search)
end
end
需要配置 Rails Current 属性(例如 Current.user)—— 参见 CurrentAttributes。
# app/resources/shared_props_resource.rb
class SharedPropsResource < ApplicationResource
one :auth, source: proc { Current }
attribute :unread_messages_count, inertia: { always: true } do
Current.user&.unread_count || 0
end
has_many :live_now, resource: LiveSessionsResource,
inertia: { once: { expires_in: 5.minutes } }
end
使用 Alba::Inertia::Controller 时,实例变量会自动序列化:
class UsersController < InertiaController
def index
@users = User.all # 通过 UserResource 自动序列化
@filters = filter_params # 普通数据直接传递
# 自动检测 UsersIndexResource
end
def show
@user = User.find(params[:id])
# 自动检测 UsersShowResource
end
end
typelize_from 告诉 Typelizer 从哪个模型推断列类型 —— 当资源名称与模型不匹配时需要。对于计算属性,需显式声明类型:
class AuthorResource < ApplicationResource
typelize_from User
attributes :id, :name, :email
typelize :string? # 下一个属性是 string | undefined
attribute :avatar_url do |user|
rails_storage_proxy_path(user.avatar) if user.avatar.attached?
end
typelize filters: "{category: number}" # 内联 TS 类型
end
当 Rails 服务器运行时类型会自动生成。手动生成:bin/rake typelizer:generate。
属性/关联上的 inertia: 选项映射到 InertiaRails 属性类型:
attribute :stats, inertia: :defer # InertiaRails.defer
has_many :users, inertia: :optional # InertiaRails.optional
has_many :countries, inertia: :once # InertiaRails.once
has_many :items, inertia: { merge: true } # InertiaRails.merge
attribute :csrf, inertia: { always: true } # InertiaRails.always
强制要求 —— 阅读整个文件 当使用分组延迟、带 match_on 的合并、滚动属性或组合多个选项时:references/prop-options.md(约 60 行)—— 包含所有 inertia: 选项变体和 inertia_prop 替代语法的完整语法。
对于基本的 inertia: :defer、inertia: :optional 或 inertia: :once 不要加载 —— 上面的简写形式已足够。
| 症状 | 原因 | 修复方法 |
|---|---|---|
TypeScript 类型全是 unknown | 缺少 typelize_from | 当资源名称与模型不匹配时,添加 typelize_from ModelName |
inertia: 选项无效 | 缺少辅助模块 | 确保 ApplicationResource 中包含 helper Alba::Inertia::Resource |
| 类型未重新生成 | 服务器未运行 | Typelizer 仅在开发环境中监视文件。手动运行 bin/rake typelizer:generate |
typelize 出现 NoMethodError | 缺少辅助模块 | 确保 ApplicationResource 中包含 helper Typelizer::DSL |
| 基于约定的渲染选择了错误的资源 | 命名不匹配 | 资源必须是 {Controller}{Action}Resource(例如,UsersController#index 对应 UsersIndexResource) |
to_inertia 未定义 | 缺少包含 | 控制器需要 include Alba::Inertia::Controller |
inertia-rails-controllers(延迟、一次性、合并、滚动)inertia-rails-typescript(globals.d.ts 中的 InertiaConfig)每周安装次数
56
代码仓库
GitHub 星标数
35
首次出现
2026年2月13日
安全审计
已安装于
codex55
gemini-cli55
opencode55
github-copilot54
amp53
kimi-cli52
Requires: alba, typelizer, alba-inertia gems in Gemfile.
Alba serializers for Inertia props with auto-generated TypeScript types. Replaces as_json(only: [...]) with structured, type-safe resources.
Before creating a resource, ask:
UserResource) — shared across pagesUsersIndexResource) — one per controller actionSharedPropsResource)NEVER:
as_json when Alba is set up — it bypasses type generation and creates untyped propstypelize_from when resource name differs from model — Typelizer can't infer column types and generates unknowndeclare module augmentations in serializers/index.ts — Typelizer-generated types go in serializers/index.ts, manual InertiaConfig goes in globals.d.ts# app/resources/application_resource.rb
class ApplicationResource
include Alba::Resource
helper Typelizer::DSL # enables typelize, typelize_from
helper Alba::Inertia::Resource # enables inertia: option on attributes
include Rails.application.routes.url_helpers
end
# app/controllers/inertia_controller.rb
class InertiaController < ApplicationController
include Alba::Inertia::Controller
inertia_share { SharedPropsResource.new(self).to_inertia }
end
# app/resources/user_resource.rb
class UserResource < ApplicationResource
typelize_from User # needed when resource name doesn't match model
attributes :id, :name, :email
typelize :string?
attribute :avatar_url do |user|
user.avatar.attached? ? rails_blob_path(user.avatar, only_path: true) : nil
end
end
# app/resources/users/index_resource.rb
# Naming convention: {Controller}{Action}Resource
class UsersIndexResource < ApplicationResource
has_many :users, resource: UserResource
typelize :string
attribute :search do |obj, _|
obj.params.dig(:filters, :search)
end
end
Requires Rails Current attributes (e.g., Current.user) to be configured — see CurrentAttributes.
# app/resources/shared_props_resource.rb
class SharedPropsResource < ApplicationResource
one :auth, source: proc { Current }
attribute :unread_messages_count, inertia: { always: true } do
Current.user&.unread_count || 0
end
has_many :live_now, resource: LiveSessionsResource,
inertia: { once: { expires_in: 5.minutes } }
end
With Alba::Inertia::Controller, instance variables auto-serialize:
class UsersController < InertiaController
def index
@users = User.all # auto-serialized via UserResource
@filters = filter_params # plain data passed through
# Auto-detects UsersIndexResource
end
def show
@user = User.find(params[:id])
# Auto-detects UsersShowResource
end
end
typelize_from tells Typelizer which model to infer column types from — needed when resource name doesn't match model. For computed attributes, declare types explicitly:
class AuthorResource < ApplicationResource
typelize_from User
attributes :id, :name, :email
typelize :string? # next attribute is string | undefined
attribute :avatar_url do |user|
rails_storage_proxy_path(user.avatar) if user.avatar.attached?
end
typelize filters: "{category: number}" # inline TS type
end
Types auto-generate when Rails server runs. Manual: bin/rake typelizer:generate.
The inertia: option on attributes/associations maps to InertiaRails prop types:
attribute :stats, inertia: :defer # InertiaRails.defer
has_many :users, inertia: :optional # InertiaRails.optional
has_many :countries, inertia: :once # InertiaRails.once
has_many :items, inertia: { merge: true } # InertiaRails.merge
attribute :csrf, inertia: { always: true } # InertiaRails.always
MANDATORY — READ ENTIRE FILE when using grouped defer, merge with match_on, scroll props, or combining multiple options: references/prop-options.md (~60 lines) — full syntax for all inertia: option variants and inertia_prop alternative syntax.
Do NOT load for basic inertia: :defer, inertia: :optional, or inertia: :once — the shorthand above is sufficient.
| Symptom | Cause | Fix |
|---|---|---|
TypeScript type is all unknown | Missing typelize_from | Add typelize_from ModelName when resource name doesn't match model |
inertia: option has no effect | Missing helper | Ensure helper Alba::Inertia::Resource is in ApplicationResource |
| Types not regenerating | Server not running | Typelizer watches files in dev only. Run bin/rake typelizer:generate manually |
inertia-rails-controllers (defer, once, merge, scroll)inertia-rails-typescript (InertiaConfig in globals.d.ts)Weekly Installs
56
Repository
GitHub Stars
35
First Seen
Feb 13, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex55
gemini-cli55
opencode55
github-copilot54
amp53
kimi-cli52
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
125,600 周安装
Claude Code 错误模式分析工具 - 批量分析历史错误与实时调试,获取可操作见解
104 周安装
Express.js 开发指南:构建 REST API 与 Web 应用 | 生产就绪最佳实践
104 周安装
Tailwind CSS 代码审计专家 | 检测间距、尺寸、响应式等反模式,优化前端性能
106 周安装
WordPress 7.0主题开发工作流:从零创建支持区块编辑器的自定义主题
106 周安装
SAELens:稀疏自编码器训练库,实现神经网络机制可解释性分析
108 周安装
PyTorch Lightning教程:高级深度学习训练框架,简化PyTorch代码,支持分布式训练
109 周安装
NoMethodError for typelize | Missing helper | Ensure helper Typelizer::DSL is in ApplicationResource |
| Convention-based rendering picks wrong resource | Naming mismatch | Resource must be {Controller}{Action}Resource (e.g., UsersIndexResource for UsersController#index) |
to_inertia undefined | Missing include | Controller needs include Alba::Inertia::Controller |