- You MUST NOT try and generate a Rails app from scratch on your own by generating each file. For a NEW app you MUST use
rails new
first to generate all of the boilerplate files necessary. - Create an app in the current directory with
rails new .
- Use Tailwind CSS for styling. Use
--css tailwind
as an option on therails new
call to do this automatically. - Use Ruby 3.2+ and Rails 8.0+ practices.
- Use the default Minitest approach for testing, do not use RSpec.
- Default to using SQLite in development.
rails new
will do this automatically but take care if you write any custom SQL that it is SQLite compatible. - An app can be built with a devcontainer such as
rails new myapp --devcontainer
but only do this if requested directly. - Rails apps have a lot of directories to consider, such as app, config, db, etc.
- Adhere to MVC conventions: singular model names (e.g., Product) map to plural tables (products); controllers are plural.
- Guard against incapable browsers accessing controllers with `allo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FuzzyIndex | |
def initialize | |
@index = Hash.new { |h, k| h[k] = Set.new } | |
end | |
def []=(key, value) | |
trigrams(key).each { @index[it] << [key, value] } | |
end | |
def [](query) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/phlex_template_handler.rb | |
require "action_view" | |
require "phlex" | |
# Intercept unknown "capitalized" method calls (e.g., PageView(...)) in templates, | |
# look up a Phlex component class, instantiate it, and render it. | |
# Crucially, we re-bind the user’s block so that `self` is the component, not the ActionView context. | |
module PhlexDynamicMethodCalls | |
def method_missing(name, *args, **kwargs, &block) | |
# Only intercept method calls starting with an uppercase letter (e.g. "PageView", "MyComponent", etc.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import logging | |
import os | |
import aiohttp | |
from typing import Annotated | |
from dotenv import load_dotenv | |
from livekit import rtc | |
from livekit.agents import ( | |
AutoSubscribe, | |
JobContext, |
Let's say you want to use Ruby for the backend of a basic webapp but React on the frontend. Here's how.
(Note: All tested on January 13, 2025 with Ruby 3.3, Sinatra 4.1.1, and React 18.3. Configs may change over time.)
First, create the app folder and set up Sinatra:
mkdir my-sinatra-react-app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# | |
# to run this file: | |
# - bundle install | |
# - ruby one_file_rails_test.rb | |
# | |
# original source: https://discuss.rubyonrails.org/t/is-it-ever-ok-to-validate-the-presence-of-a-primary-key-of-a-belongs-to-association/84904 | |
# |
# In your Gemfile
gem "localhost"
# Then, depending on your desired server
gem "falcon"
gem "puma"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Maybe there's a way to support ActiveSupport::Concern's dependencies too? | |
# Although this doesn't use the module hierarchy, so `prepend` can't work. | |
class Concern < Module | |
def initialize(&block) = @block = block | |
def included(klass) = klass.class_eval(&@block) | |
end | |
module Kernel | |
def Concern(...) = Concern.new(...) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Abort sign off on any error | |
set -e | |
# Start the benchmark timer | |
SECONDS=0 | |
# Repository introspection | |
OWNER=$(gh repo view --json owner --jq .owner.login) |
NewerOlder