Forme: The Flexible, Dependency-Free HTML Form Library for Ruby
Share this article
For Ruby developers tired of heavyweight form libraries, Forme offers a breath of fresh air. Created by Jeremy Evans, this elegantly designed HTML form generator prioritizes flexibility and simplicity while eliminating external dependencies. Its secret weapon? An internal abstract syntax tree that compiles to multiple output formats—enabling powerful features like dynamic read-only views without code duplication.
Core Philosophy & Key Features
Forme's design centers on five principles:
- Zero Dependencies: Pure Ruby implementation
- Simple API: Intuitive builder syntax
- Object-Agnostic: Works with or without model objects
- Output Flexibility: Compiles to HTML, plain text, or customized formats
- Framework Integration: First-class support for Roda, Rails, and Sinatra
# Object-backed form example (Sequel model)
Forme.form(Album[1], action: '/update') do |f|
f.input :name
f.input :artist
f.subform :tracks do
f.input :title
end
end
The AST Advantage
Forme's abstract syntax tree enables transformative workflows. The same form definition can render as editable fields or read-only displays simply by changing the formatter:
# Editable form
Forme.form(album, action: '/edit')
# Read-only view
Forme.form(album, formatter: :readonly)
This compiles <input> elements to <span> counterparts, ideal for admin dashboards requiring dual views.
Deep Sequel Integration
Forme shines when paired with Sequel ORM. It automatically handles:
- Label generation from column names
- Validation attributes (maxlength, pattern) from model validations
- Association management (many-to-one, many-to-many)
- Nested attributes via subform method
f.input :artist, as: :radio # Radio buttons instead of select
f.input :tags, as: :checkbox # Checkbox group for associations
The optional forme_set plugin adds security protections by validating submitted values against form-rendered options—preventing tampering with association IDs.
Framework Synergy
Forme elegantly adapts to your stack:
- Roda: CSRF protection via forme_route_csrf plugin
- Rails: forme/rails helper for ERB templates
- Sinatra: Dedicated extension with ERB support
<%# Rails ERB integration %>
<%= forme(@album) do |f| %>
<%= f.input :name %>
<% end %>
Advanced Architecture
Under the hood, Forme employs a transformer pipeline:
1. Formatters: Convert high-level Input objects to Tag nodes
2. Labelers/Error Handlers: Enhance tags with metadata
3. Wrappers: Structure output (fieldsets, divs, etc.)
4. Serializers: Generate final HTML/text
Customize behavior via configurations:
Forme.register_config(:custom,
wrapper: :div,
labeler: :explicit
)
Why Choose Forme Over Alternatives?
Unlike Formtastic or SimpleForm, Forme offers:
- Zero runtime dependencies
- Output-agnostic rendering (HTML, PDF, etc.)
- Read-only transformation without code duplication
- First-class Roda support
"The AST architecture lets us repurpose form logic in ways traditional libraries can't. It's like JSX for Ruby templates." – Core maintainer
Getting Started
Installation:
gem install forme
Explore resources:
- GitHub Repository
- Live Demo
- RDoc Documentation
For teams valuing flexibility, security, and clean abstraction, Forme delivers a uniquely powerful approach to form handling in the Ruby ecosystem.
Source: Forme GitHub Repository