Catalog / Sinatra Cheat Sheet
Sinatra Cheat Sheet
A concise reference for Sinatra, a lightweight Ruby web framework. Covers essential concepts, routing, views, and helpers for quick development.
Core Concepts & Setup
Installation
Install Sinatra via RubyGems:
|
For logging:
|
Basic Structure
A simple Sinatra application structure:
|
Running the app:
|
Configuration
|
Sets the port the application listens on. |
|
Sets the environment (development, production, test). |
|
Enables session support. |
Routing
HTTP Methods
|
Handles GET requests. |
|
Handles POST requests. |
|
Handles PUT requests. |
|
Handles DELETE requests. |
|
Handles PATCH requests. |
|
Handles HEAD requests. |
|
Handles OPTIONS requests. |
Route Parameters
Accessing parameters from the route:
|
Optional parameters:
|
Splats (capturing multiple segments):
|
Views & Templates
Rendering Views
Rendering a view:
This will render |
Specifying a different template path:
|
Template Engines
|
Embedded Ruby templates. |
|
Haml templates (requires |
|
Slim templates (requires |
|
Liquid templates (requires |
|
Markdown templates (requires |
Layouts
Using a layout:
This will render |
Disabling layout:
|
Helpers & Extensions
Defining Helpers
Defining a helper:
|
Built-in Helpers
|
Immediately stops request processing. |
|
Passes control to the next matching route. |
|
Redirects the client to the given URL. |
|
Access the session hash (if sessions are enabled). |
|
Access request parameters. |
Sinatra Extensions
Using extensions:
|
Common extensions:
|