Skip to content

Coming from Rails ​

Runcobo will feel familiar: actions with before and after filters, Jbuilder views, migrations, a console and one command to run the app. The big differences come from Crystal being compiled — routes, input types, templates and SQL are all checked before the app starts — and from Runcobo using SQL instead of Active Record.

At a glance ​

RailsRuncobo
config/routes.rb + a controller methodOne action class per endpoint, declaring its own route
params.require(:post).permit(:title)A contract + validate — typed input, or an automatic 422
before_action / skip_before_actionbefore / skip
after_actionafter
ApplicationControllerBlog::BaseAction
render json: postrender_json(...), or a .jbuilder template with render
Jbuilder viewsJbuilder templates — same idea, compiled in
ERB views and layoutsWater templates and layouts
head :no_contentrender_empty status_code: 204
Active Record modelsNamed SQL queries → a typed store
Post.includes(:comments)One batch query with ANY(...) (N+1)
db/migrate/*.rbPlain SQL files in db/migrations/
db/seeds.rbseed_command in runcobo.yml
Request specsCrystal spec + dispatch (Testing)
CommandRuncobo
rails new blogcrystal init app blog + bin/cobo init
rails serverbin/cobo dev (rebuilds and restarts on save)
rails consolebin/cobo console
rails runner script.rbbin/cobo runner script.cr
rails routesbin/cobo routes
rails db:create db:migratebin/cobo db prepare
rails db:migrate / db:rollbackbin/cobo db migrate / db rollback
rails test / rspecbin/cobo test

A controller becomes actions ​

In Rails, one controller holds every endpoint for a resource:

app/controllers/posts_controller.rb
ruby
class PostsController < ApplicationController
  before_action :authenticate, only: [:create]

  def show
    @post = Post.find(params[:id])
  rescue ActiveRecord::RecordNotFound
    render json: {error: "post_not_found"}, status: :not_found
  end

  def create
    post = Post.create!(params.require(:post).permit(:title, :body))
    render json: post, status: :created
  end
end

In Runcobo, each endpoint is a class with its route, its input and its response next to it:

src/actions/posts/show_post.cr
crystal
class Posts::ShowPost < Blog::BaseAction
  get "/posts/:id"

  call do
    input = validate(ShowPostContract)          # id is an Int64, or the client gets a 422
    if post = store.get_post?(id: input.id)     # nil when not found: no exception
      render                                    # → show_post.jbuilder
    else
      render_json({error: "post_not_found"}, status_code: 404)
    end
  end
end
src/actions/posts/create_post.cr
crystal
class Posts::CreatePost < Blog::AdminAction     # the admin base class authenticates
  post "/posts"

  call do
    input = validate(CreatePostContract)
    post = store.create_post(title: input.title, body: input.body)
    render status_code: 201
  end
end

There's no only:/except: on filters: actions that need authentication inherit from a base class that has it, and the rest don't. See Filters.

Strong parameters become contracts ​

A contract declares every field, its type and its constraints, and covers the path, query and body together:

src/actions/posts/create_post_contract.cr
crystal
class Posts::CreatePostContract < RequestValidation::Contract
  schema do
    required :title, String, filled: true, max_size: 200
    required :body, String, filled: true
  end
end

input.title is a String, not a value you still have to check. Invalid input never reaches your code: the client gets a 422 listing every problem. Undeclared fields are dropped, so there's nothing to permit. JSON isn't wrapped in a root key unless you declare one.

Active Record becomes SQL ​

Instead of models, you write the queries you need, and bin/cobo generate turns each into a typed method:

db/queries/posts.sql
sql
-- name: GetPost :one
SELECT id, title, body, created_at FROM posts WHERE id = sqlc.arg(id);
crystal
store.get_post?(id: 1)   # => Blog::Store::GetPostRow? — title is a String, created_at a Time
Active RecordRuncobo
Post.find(id)store.get_post?(id: id) — nil instead of an exception
Post.where(status: "draft").order(id: :desc)A named query with WHERE and ORDER BY
post.update(title: "x")An UPDATE ... RETURNING query (partial updates)
post.commentsA query for the comments, or a JOIN
Post.includes(:comments)WHERE post_id = ANY(...) + group_by (N+1)
Post.transaction dostore.transaction do (recipe)
Model validationsContracts for input, constraints in the schema for data
CallbacksExplicit calls in the action, or SQL defaults and triggers

The Query Recipes page has more translations.

Migrations are SQL ​

db/migrations/20261201093000_create_posts.sql
sql
-- migrate:up
CREATE TABLE posts (
  id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  title text NOT NULL,
  body text NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now()
);

-- migrate:down
DROP TABLE posts;

There's no schema.rb: the migrations are the schema, and code generation reads them directly. See Migrations.

Views ​

Jbuilder templates look like Rails', with explicit locals for partials:

src/actions/posts/show_post.jbuilder
crystal
json.id post.id
json.title post.title
json.partial! "posts/partials/author", author: author

They can use the local variables of call (post above) — there are no @instance variables to pass. HTML uses Water instead of ERB. A missing template or a typo in a variable is a compile error.

What's different ​

  • No environments in the app. Configuration comes from environment variables, which cobo dev, cobo test and your deploy set — see runcobo.yml.
  • No autoloading. src/app.cr requires the app; require "./actions/**" picks up every action.
  • No sessions or CSRF tokens built in. Runcobo is API-first; see Security if you render forms.
  • Exceptions aren't control flow. Lookups return nil; render the 404 yourself.
  • Compile, then run. cobo dev rebuilds on every save and keeps the old version running if the new one doesn't compile.

Next: the blog tutorial shows a whole application.

Released under the MIT License.