Rails w/ SQLite

Start tinkering

Full-blown Rails app with SQLite database (in-memory). No setup required.

require "bundler/inline"

gemfile do
  source "https://rubygems.org"

  gem "rails"
  gem "sqlite3"
  gem "puma"
  # add gems you need here
end

# using SQLite in memory; mind that's only working with max. 1 connection
require "active_record"
require "action_controller/railtie"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:", pool: 1)
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :notes do |t|
    t.string :title
    t.text :body
    t.timestamps
  end
end

class Note < ActiveRecord::Base
  validates :title, presence: true
end

Note.create(title: "Hello World!", body: "Greetings from Single-File Rails 👋")
Note.create(title: "Reminder", body: "You need to stop and restart for changes to take effect")
Note.create(title: "Missing Feature", body: %(Sadly the <a class="text-blue-400 hover:text-blue-600" href="https://github.com/wolfgangrittner/keepgoing">keepgoing gem</a> does not play nicely with Rails yet.))
Note.create(title: "Still great", body: "This is still a great way to quickly try something out 🎉<br>Even with full-blown Rails 🛤")

# Put back the db connection, because there's only 1, remember
ActiveRecord::Base.connection_pool.checkin(ActiveRecord::Base.connection)

class InlineApp < Rails::Application
  secrets.secret_token    = "secret_token"
  secrets.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  Rails.logger = config.logger

  routes.draw do
    resources :notes, only: %i[index create]
    root to: redirect('/notes')
  end
end

class NotesController < ActionController::Base
  include Rails.application.routes.url_helpers

  def index
    @notes = Note.all
    render inline: notes_html
  end

  def create
    Note.create(title: "New Note #{Note.count + 1}", body: "This is your new note")
    redirect_to notes_path
  end

  private

  def notes_html
    <<~ERB
      <!DOCTYPE html>
      <title>Single-File Rails</title>
      <script src="https://cdn.tailwindcss.com"></script>
      <body class="bg-zinc-50 flex gap-5 p-5 flex-wrap">
        <h1 class="font-bold text-xl text-red-500 w-full">Single-File Rails Notes</h1>
        <% @notes.each do |note| %>
          <div class="bg-white rounded shadow p-5 w-full sm:max-w-sm">
            <h2 class="font-bold text-emerald-400 mb-1"><%= note.title %></h1>
            <p><%= note.body.html_safe %></p>
          </div>
        <% end %>
        <%= form_with url: notes_path, class: "w-full" do |form| %>
          <%= form.submit "Create New Note", class: "rounded bg-emerald-300 px-3 py-1 border border-emerald-500 hover:bg-emerald-400 text-emerald-900" %>
        <% end %>
      </body>
    ERB
  end
end

require "puma"
require "rails/command"
require "rails/commands/server/server_command"

options = {
  app: InlineApp,
  Port: 3000,
  min_threads: 1,
  max_threads: 1
}
Rails::Server.new(options).start