Skip to content

Getting Started with Trails ​

This guide takes you from a checkout of the trails repository to a running application with one resource, Post, that you can list, create, edit and delete in the browser. Along the way it generates a model and its migration, a controller with its views and routes, and starts the development server.

Every command and output block below is included from the README Quickstart when this site is built. CI runs that Quickstart command by command and diffs each output block against what the run prints (scripts/quickstart/run.sh), so what you read here is what the current main does.

trails is TypeScript, so there is no Ruby to install. You need Node 26 or later and pnpm, which corepack provides.

Build the framework ​

trails is not on the npm registry yet, so an application links against a local checkout. Clone it, install its dependencies and compile it once:

sh
git clone https://github.com/blazetrailsdev/trails.git
cd trails
npm install -g corepack   # Node 26 no longer bundles corepack
corepack enable
pnpm install
pnpm build            # tsc --build over the workspace, ~10s
export TRAILS=$PWD

$TRAILS now names the checkout; later steps use it to find the trails executable and the packages.

Create an application ​

trails new lays out an application the way rails new does: app/ for models, controllers and views, config/ for routes, the database and the environments, and db/ for migrations and the schema. Templates are .tse files, trails' typed counterpart to ERB.

sh
cd ..
node $TRAILS/packages/trailties/bin/trails.js new blog --skip-install --skip-git
cd blog
text
      create  package.json
      create  tsconfig.json
      ...
      create  config/routes.ts
      create  config/database.ts
      create  app/controllers/application-controller.ts
      create  app/models/application-record.ts
      create  app/views/layouts/application.html.tse
      ...

The generated package.json asks for @blazetrails/* packages from the npm registry (the alpha builds above). To run against your checkout instead, point them at it with pnpm overrides, then install:

sh
{
  echo "overrides:"
  for p in $TRAILS/packages/*; do
    echo "  \"$(node -p "require('$p/package.json').name")\": \"link:$p\""
  done
  echo "  vitest: \"$(node -p "require('$TRAILS/node_modules/vitest/package.json').version")\""
  printf 'allowBuilds:\n  better-sqlite3: true\n  esbuild: true\n'
} > pnpm-workspace.yaml
node -e '
const fs = require("fs"), app = JSON.parse(fs.readFileSync("package.json"));
const meta = require(process.env.TRAILS + "/packages/trails/package.json");
for (const name of Object.keys(meta.dependencies))
  app.dependencies[name] = `link:${process.env.TRAILS}/packages/${name.slice(13)}`;
fs.writeFileSync("package.json", JSON.stringify(app, null, 2) + "\n");
'
pnpm install

From here on, run commands from inside blog/. bin/trails is the application's own copy of the command line.

Generate a model, a controller and routes ​

The scaffold generator writes everything one resource needs: a migration and a model, a controller with the seven REST actions, its views, a resources("posts") line in config/routes.ts, a helper and tests. Running the migration creates the posts table in the development SQLite database and dumps the result to db/schema.ts.

sh
bin/trails generate scaffold Post title:string body:text
text
      invoke  active_record
      create    db/migrate/20261023194933_create_posts.ts
      create    app/models/post.ts
      invoke    test_unit
      create      test/models/post.test.ts
      create      test/fixtures/posts.yml
      invoke  resource_route
       route    this.resources("posts");
      invoke  scaffold-controller
      create    app/controllers/posts-controller.ts
      invoke    tse
      create      app/views/posts
      create      app/views/posts/index.html.tse
      create      app/views/posts/edit.html.tse
      create      app/views/posts/show.html.tse
      create      app/views/posts/new.html.tse
      create      app/views/posts/_form.html.tse
      create      app/views/posts/_post.html.tse
      invoke    resource_route
      invoke    test_unit
      create      test/controllers/posts-controller.test.ts
      create      test/system/posts.test.ts
      invoke    helper
      create      app/helpers/posts-helper.ts
      invoke      test_unit
sh
pnpm db:migrate
text
== 20261023194933 CreatePosts: migrating ======================================
-- createTable("posts")
   -> 0.0082s
== 20261023194933 CreatePosts: migrated (0.0112s) =============================

The migration subclasses Migration.get(8.0), Rails' ActiveRecord::Migration[8.0], and the migrate writes db/schema.ts from the live database, as Rails writes schema.rb:

ts
// db/migrate/20261023194933_create_posts.ts
import { Migration } from "@blazetrails/activerecord";

export class CreatePosts extends Migration.get(8.0) {
  async change(): Promise<void> {
    await this.createTable("posts", (t) => {
      t.string("title");
      t.text("body");

      t.timestamps();
    });
  }
}

Talk to the database ​

The model has no attribute declarations: title and body are read from the table when the class first touches it. Queries are async, so every call that reaches the database is awaited, while building a relation (where, order, limit) is not.

ts
// app/models/post.ts (generated)
import { ApplicationRecord } from "./application-record.ts";

export class Post extends ApplicationRecord {
}
ts
// script/hello.ts
import "../config/environment.ts";
import { Post } from "../app/models/post.ts";

const post = await Post.create({ title: "Hello", body: "First post" });
console.log(post.id, post.isPersisted());

console.log(await Post.count());
console.log((await Post.where({ title: "Hello" }).first())?.body);
console.log(Post.where({ title: "Hello" }).order("created_at").limit(10).toSql());
sh
node script/hello.ts
text
1 true
1
First post
SELECT "posts".* FROM "posts" WHERE "posts"."title" = 'Hello' ORDER BY created_at LIMIT 10
sh
bin/trails routes
text
                                   Prefix Verb   URI Pattern                                                                                        Controller#Action
                                    posts GET    /posts(.:format)                                                                                   posts#index
                                          POST   /posts(.:format)                                                                                   posts#create
                                 new_post GET    /posts/new(.:format)                                                                               posts#new
                                edit_post GET    /posts/:id/edit(.:format)                                                                          posts#edit
                                     post GET    /posts/:id(.:format)                                                                               posts#show
                                          PATCH  /posts/:id(.:format)                                                                               posts#update
                                          PUT    /posts/:id(.:format)                                                                               posts#update
                                          DELETE /posts/:id(.:format)                                                                               posts#destroy
                      trails_health_check GET    /up(.:format)                                                                                      trails/health#show
           trails_postmark_inbound_emails POST   /trails/action_mailbox/postmark/inbound_emails(.:format)                                           action_mailbox/ingresses/postmark/inbound_emails#create
              trails_relay_inbound_emails POST   /trails/action_mailbox/relay/inbound_emails(.:format)                                              action_mailbox/ingresses/relay/inbound_emails#create
           trails_sendgrid_inbound_emails POST   /trails/action_mailbox/sendgrid/inbound_emails(.:format)                                           action_mailbox/ingresses/sendgrid/inbound_emails#create
     trails_mandrill_inbound_health_check GET    /trails/action_mailbox/mandrill/inbound_emails(.:format)                                           action_mailbox/ingresses/mandrill/inbound_emails#healthCheck
           trails_mandrill_inbound_emails POST   /trails/action_mailbox/mandrill/inbound_emails(.:format)                                           action_mailbox/ingresses/mandrill/inbound_emails#create
            trails_mailgun_inbound_emails POST   /trails/action_mailbox/mailgun/inbound_emails/mime(.:format)                                       action_mailbox/ingresses/mailgun/inbound_emails#create
          trails_conductor_inbound_emails GET    /trails/conductor/action_mailbox/inbound_emails(.:format)                                          trails/conductor/action_mailbox/inbound_emails#index
                                          POST   /trails/conductor/action_mailbox/inbound_emails(.:format)                                          trails/conductor/action_mailbox/inbound_emails#create
       new_trails_conductor_inbound_email GET    /trails/conductor/action_mailbox/inbound_emails/new(.:format)                                      trails/conductor/action_mailbox/inbound_emails#new
           trails_conductor_inbound_email GET    /trails/conductor/action_mailbox/inbound_emails/:id(.:format)                                      trails/conductor/action_mailbox/inbound_emails#show
new_trails_conductor_inbound_email_source GET    /trails/conductor/action_mailbox/inbound_emails/sources/new(.:format)                              trails/conductor/action_mailbox/inbound_emails/sources#new
   trails_conductor_inbound_email_sources POST   /trails/conductor/action_mailbox/inbound_emails/sources(.:format)                                  trails/conductor/action_mailbox/inbound_emails/sources#create
   trails_conductor_inbound_email_reroute POST   /trails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)                               trails/conductor/action_mailbox/reroutes#create
trails_conductor_inbound_email_incinerate POST   /trails/conductor/action_mailbox/:inbound_email_id/incinerate(.:format)                            trails/conductor/action_mailbox/incinerates#create
                      trails_service_blob GET    /trails/active_storage/blobs/redirect/:signed_id/*filename(.:format)                               active_storage/blobs/redirect#show
                trails_service_blob_proxy GET    /trails/active_storage/blobs/proxy/:signed_id/*filename(.:format)                                  active_storage/blobs/proxy#show
                                          GET    /trails/active_storage/blobs/:signed_id/*filename(.:format)                                        active_storage/blobs/redirect#show
               trails_blob_representation GET    /trails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
         trails_blob_representation_proxy GET    /trails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format)    active_storage/representations/proxy#show
                                          GET    /trails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)          active_storage/representations/redirect#show
                      trails_disk_service GET    /trails/active_storage/disk/:encoded_key/*filename(.:format)                                       active_storage/disk#show
               update_trails_disk_service PUT    /trails/active_storage/disk/:encoded_token(.:format)                                               active_storage/disk#update
                    trails_direct_uploads POST   /trails/active_storage/direct_uploads(.:format)                                                    active_storage/direct_uploads#create

The console loads every model before it prompts:

sh
bin/trails console
text
trails> const p = await Post.first()
undefined
trails> p.title
'Hello'
trails> await Post.where({ title: "Hello" }).count()
1

Start the server ​

sh
pnpm prepare                # builds app/views, the scaffold's views included
bin/trails server -p 3927   # the default port is 3000; this run used 3927
text
=> Trails application starting in development on http://localhost:3927
=> Vite dev server with HMR enabled
=> Ctrl+C to stop

Visit /posts, create a post, edit it and delete it. A validation added to the model shows up as form errors after a restart; a change to a view shows up on the next request.

Where to go next ​

  • Trails Idioms translates the naming, async, options-object and block conventions every other guide relies on.
  • The package pages list each package's ratified deviations from Rails.
  • The examples run the same kind of application on a single server, on AWS Lambda and on Cloudflare Workers.

This guide is licensed under CC BY 4.0. Code examples are licensed under the MIT License.