alba-inertia
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAlba + Typelizer for Inertia Rails
用于Inertia Rails的Alba + Typelizer
Requires: , , gems in Gemfile.
albatypelizeralba-inertiaAlba serializers for Inertia props with auto-generated TypeScript types.
Replaces with structured, type-safe resources.
as_json(only: [...])Before creating a resource, ask:
- Reusable data shape (user, course)? → Entity resource () — shared across pages
UserResource - Page-specific props bundle? → Page resource () — one per controller action
UsersIndexResource - Global data (auth, notifications)? → Shared props resource ()
SharedPropsResource
NEVER:
- Use when Alba is set up — it bypasses type generation and creates untyped props
as_json - Skip when resource name differs from model — Typelizer can't infer column types and generates
typelize_fromunknown - Put augmentations in
declare module— Typelizer-generated types go inserializers/index.ts, manual InertiaConfig goes inserializers/index.tsglobals.d.ts
要求:Gemfile中需包含、、这几个gem。
albatypelizeralba-inertia为Inertia props提供Alba序列化器,并自动生成TypeScript类型。
使用结构化、类型安全的资源替代。
as_json(only: [...])创建资源前,请确认:
- 是否为可复用的数据结构(如用户、课程)? → 实体资源()——可跨页面共享
UserResource - 是否为页面专属的props集合? → 页面资源()——每个控制器动作对应一个
UsersIndexResource - 是否为全局数据(如认证、通知)? → 共享props资源()
SharedPropsResource
禁止操作:
- 当Alba已配置完成时,请勿使用——它会绕过类型生成,导致props无类型
as_json - 当资源名称与模型名称不一致时,请勿省略——Typelizer无法推断列类型,会生成
typelize_from类型unknown - 请勿将增强代码放入
declare module——Typelizer生成的类型需放在serializers/index.ts中,手动配置的InertiaConfig需放在serializers/index.ts中globals.d.ts
Setup
配置步骤
ApplicationResource (all resources inherit from this)
ApplicationResource(所有资源都继承自该类)
ruby
undefinedruby
undefinedapp/resources/application_resource.rb
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
undefinedclass 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
undefinedController Integration
控制器集成
ruby
undefinedruby
undefinedapp/controllers/inertia_controller.rb
app/controllers/inertia_controller.rb
class InertiaController < ApplicationController
include Alba::Inertia::Controller
inertia_share { SharedPropsResource.new(self).to_inertia }
end
undefinedclass InertiaController < ApplicationController
include Alba::Inertia::Controller
inertia_share { SharedPropsResource.new(self).to_inertia }
end
undefinedResource Types
资源类型
Entity Resource (reusable data shape)
实体资源(可复用的数据结构)
ruby
undefinedruby
undefinedapp/resources/user_resource.rb
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
undefinedclass 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
undefinedPage Resource (page-specific props)
页面资源(页面专属props)
ruby
undefinedruby
undefinedapp/resources/users/index_resource.rb
app/resources/users/index_resource.rb
Naming convention: {Controller}{Action}Resource
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
undefinedclass UsersIndexResource < ApplicationResource
has_many :users, resource: UserResource
typelize :string
attribute :search do |obj, _|
obj.params.dig(:filters, :search)
end
end
undefinedShared Props Resource
共享Props资源
ruby
undefinedruby
undefinedapp/resources/shared_props_resource.rb
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
undefinedclass 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
undefinedConvention-Based Rendering
基于约定的渲染方式
With , instance variables auto-serialize:
Alba::Inertia::Controllerruby
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集成后,实例变量会自动序列化:
Alba::Inertia::Controllerruby
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
endTypelizer + Type Generation
Typelizer + 类型生成
typelize_fromruby
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
endTypes auto-generate when Rails server runs. Manual: .
bin/rake typelizer:generatetypelize_fromruby
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在Rails服务器运行时,类型会自动生成。手动生成命令:。
bin/rake typelizer:generateInertia Prop Options in Alba
Alba中的Inertia Prop选项
The option on attributes/associations maps to prop types:
inertia:InertiaRailsruby
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.alwaysMANDATORY — READ ENTIRE FILE when using grouped defer, merge with ,
scroll props, or combining multiple options:
(~60 lines) — full syntax
for all option variants and alternative syntax.
match_onreferences/prop-options.mdinertia:inertia_propDo NOT load for basic , , or
— the shorthand above is sufficient.
inertia: :deferinertia: :optionalinertia: :once属性/关联上的选项对应的prop类型:
inertia:InertiaRailsruby
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强制要求——当使用分组延迟、带的合并、滚动props或组合多个选项时,请通读整个文档:
(约60行)——包含所有选项变体的完整语法,以及替代语法。
match_onreferences/prop-options.mdinertia:inertia_prop若仅使用基础的、或,则无需查看该文档——上述简写语法已足够。
inertia: :deferinertia: :optionalinertia: :onceTroubleshooting
故障排查
| Symptom | Cause | Fix |
|---|---|---|
TypeScript type is all | Missing | Add |
| Missing helper | Ensure |
| Types not regenerating | Server not running | Typelizer watches files in dev only. Run |
| Missing helper | Ensure |
| Convention-based rendering picks wrong resource | Naming mismatch | Resource must be |
| Missing include | Controller needs |
| 症状 | 原因 | 解决方法 |
|---|---|---|
TypeScript类型全为 | 缺少 | 当资源名称与模型名称不一致时,添加 |
| 缺少helper | 确保 |
| 类型未自动重新生成 | 服务器未运行 | Typelizer仅在开发环境监听文件变化。请手动运行 |
调用 | 缺少helper | 确保 |
| 基于约定的渲染选择了错误的资源 | 命名不匹配 | 资源名称必须遵循 |
| 缺少include | 控制器需包含 |
Related Skills
相关技能
- Prop types → (defer, once, merge, scroll)
inertia-rails-controllers - TypeScript config → (InertiaConfig in globals.d.ts)
inertia-rails-typescript
- Prop类型 → (defer、once、merge、scroll)
inertia-rails-controllers - TypeScript配置 → (globals.d.ts中的InertiaConfig)
inertia-rails-typescript