Setup Sinatra Application
This guide walks you through setting up a Sinatra web application from scratch. Sinatra is a lightweight Ruby web framework that allows you to quickly create web applications with minimal effort and configuration.
Create Project Foundation
- Create a new project directory:
- Create the Gemfile to manage dependencies:
echo "source \"https://rubygems.org\"
git_source(:github) { |repo| \"https://github.com/\#{repo}.git#\" }" > Gemfile
The Gemfile defines your application's gem dependencies and where to fetch them from.
Install Required Gems
- Add necessary gems to your application:
bundle add builder i18n kramdown pg puma rack rack-test require_all rspec sassc sinatra sinatra-contrib slim tux
These gems provide essential functionality: - sinatra: The core framework - sinatra-contrib: Extensions for Sinatra - slim: Template engine - puma: Web server - i18n: Internationalization support - kramdown: Markdown processing
Create Project Structure
- Set up the basic directory structure:
- Create key files for your application:
# Main application file
touch app.rb
# Rack configuration
touch config.ru
# View templates
touch views/layout.slim views/index.slim views/content.slim views/404.slim
# Static assets
touch public/content.md public/favicon.ico public/icon.png public/icon.svg
touch public/site.webmanifest public/robots.txt
# JavaScript and CSS
touch public/assets/js/app.js public/assets/css/main.scss
# Localization files
touch locales/en/en.yml locales/fr/fr.yml
# Library modules
touch lib/sitemap.rb
# Test files
touch spec/app_spec.rb
This structure follows Sinatra conventions with separate directories for views, public assets, and library code.
Configure Rack Application
- Create the
config.ru
file for Rack:
The config.ru file tells Rack how to run your application and loads necessary dependencies.
Build Main Application
- Create the core application file
app.rb
:
This modular application sets up configuration, helpers for internationalization, and routes for the application.
Create View Templates
- Create a layout template in
views/layout.slim
:
layout.slim | |
---|---|
- Create the homepage in
views/index.slim
:
index.slim | |
---|---|
Templates use the Slim syntax for a cleaner, more readable format compared to HTML. The templates also use the
t
helper for internationalization.
Set Up Internationalization
- Create English translations in
locales/en/en.yml
:
en.yaml | |
---|---|
- Create French translations in
locales/fr/fr.yml
:
fr.yaml | |
---|---|
These YAML files define content in different languages that can be accessed using the
t
helper in views.
Add Markdown Support
- Create sample markdown content in
public/content.md
:
# Markdown Content
This is a sample Markdown file that will be rendered in HTML.
## Features
- **Bold text** and *italic text*
- Lists and sublists
- [Links](http://example.com)
- Code samples
- Create the content view in
views/content.slim
:
This setup allows you to render Markdown files as HTML using the Kramdown gem.
Set Up Testing
- Initialize RSpec:
- Create a basic test in
spec/app_spec.rb
:
Testing ensures your application works as expected and helps catch regressions.
Run Your Application
- Start the Sinatra application:
- Access your application at: http://localhost:9292
Puma is a fast and concurrent web server for Ruby applications.
Prepare for Deployment
- Specify your Ruby version in the Gemfile:
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git#" }
ruby "3.2.5"
# Gems...
- Add platform specifications for deployment:
These steps prepare your application for deployment to services like Heroku.
Access Your Application
Once deployed, you can access different versions of your application:
- English Version:
http://your-app.com/?locale=en
- French Version:
http://your-app.com/?locale=fr
- Markdown Content:
http://your-app.com/from-file
Your application now supports multiple languages and can render Markdown content.