Documentation

Controllers

Updated
Browse documentation

Published 2026-08-13. V2 is a prerelease; the web core is release-gated and other previews are named separately. What beta means.

Read this page as HTML, Markdown, or structured JSON—or open the published Markdown with an AI assistant. Gemini receives the prompt through your clipboard because its signed-out page does not reliably prefill URL text; paste when the new tab opens. External assistants need the public site URL.

Controllers

A controller action turns an HTTP request into a response. Amber creates the controller selected by the router, runs its filters, calls the action, and finalizes the response through the active pipeline.

Run from: the application root.

Terminal
amber generate controller Posts index show

The generator writes Crystal controller code and ECR views, but it deliberately does not guess routes. For the command above it creates src/controllers/posts_controller.cr, src/views/posts/index.ecr, and src/views/posts/show.ecr.

File: config/routes.cr — add these routes inside the existing Amber::Server.configure block.

Crystal
Amber::Server.configure do
  routes :web do
    get "/posts", PostsController, :index
    get "/posts/:id", PostsController, :show
  end
end

Actions and views

File: src/controllers/posts_controller.cr — replace the generated action bodies with the application behavior. Keep the class inside this file.

Crystal
class PostsController < ApplicationController
  def index
    title = "Recent posts"
    render("index.ecr")
  end

  def show
    post_id = params[:id]
    render("show.ecr")
  end
end

Local variables remain available to the ECR template rendered by the action. Keep request parsing and authorization in explicit boundaries; use the Schema API when input needs typed validation.

Amber's resources macro uses the conventional action names index, new, create, show, edit, update, and destroy. Ordinary actions may use any name when registered explicitly.

Controller interfaces

V2 web output is ECR. Examples that require .slang templates belong to the V1 documentation and should not be copied into a new V2 application.