# Brought to you by Ruby Starters - https://starters.wolfgangrittner.dev
# Author: Wolfgang Rittner
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 keepgoing gem does not play nicely with Rails yet.))
Note.create(title: "Still great", body: "This is still a great way to quickly try something out 🎉
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
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
<%= note.body.html_safe %>