Rails Quick Scripts

These quick recipes assume you are starting with a new Rails 7 project from scratch. Because they do quick find & replace to add content to existing files, they are guaranteed to work only if you start with a new Rails app. (Currently tested against Rails 7.) Most developers should pay attention to the Gems & setup you are adding to your codebases, but these quick scripts are great as a teaching tool and for setting demo apps up quickly.

1/ Rails 7 JS Strategy Setup (pick one)

JSBundling using the Currently Selected Node Version

Using whatever your current Node Version is (Must have NVM installed). Be sure to switch to the desired Ruby and Node versions. This setup fixes the .ruby-version file by removing the “ruby-” prefix that is installed by default with Rails. (Supported universally by RVM, Rbenv, and asdf). Just replace YourAppNameHere with the name of your new app.

APP_NAME=YourAppNameHere && rails new $APP_NAME --javascript=esbuild --database=postgresql && CURRENT_NODE=$(nvm current) && cd $APP_NAME && echo $CURRENT_NODE >> .node-version && echo "Node + Ruby versions are in \`.node-version\` and \`.ruby-version\`, respectively.\n\n# Setup\n\n\`./bin/setup\`\n\n# Start Rails\n\n\`./bin/dev\`\n\n# Run Specs\n\nrun with \`./bin/rake\`" > README.md && git add . && git commit -m "initial commit with $(rails -v), Node $CURRENT_NODE, Ruby $(more ./.ruby-version)" && sed -i '' -e 's/ruby-//g' .ruby-version && RUBY_STRING="ruby \"$(more ./.ruby-version)\"" && sed -i '' -e "s/$RUBY_STRING/ruby File.read('.ruby-version').strip/g" Gemfile  && git add . && git commit -m "fixes .ruby-version file and sets Gemfile to use .ruby-version file" && ./bin/setup && git add . && git commit -m "adds schema file"

Start up your app with ./bin/dev

ImportMap — No Node. Use this setup for a Node-free Rails 7 app.

APP_NAME=YourAppNameHere && rails new $APP_NAME --database=postgresql && cd $APP_NAME && git add . && git commit -m "initial commit with $(rails -v), Ruby $(more ./.ruby-version)" && sed -i '' -e 's/ruby-//g' .ruby-version && RUBY_STRING="ruby \"$(more ./.ruby-version)\"" && sed -i '' -e "s/$RUBY_STRING/ruby File.read('.ruby-version').strip/g" Gemfile  && git add . && git commit -m "fixes .ruby-version file and set Gemfile to use .ruby-version file" && ./bin/setup && git add . && git commit -m "adds schema file"

Start up your app with rails server

2/ Testing Setup (Always Install)

Adds Dotenv, Rspec, Rspec-Wait, FactoryBot, FFaker, VCR, SimpleCov, and Removes Minitest

Adds to and modifies .gitignore for DotEnv, coverage reports. Installs Rspec and configures it with FactoryBot. Sets up coverage reports to print out automatically after every test (see coverage/ — a folder that is not checked in to your repo). This setup is for Rspec and removes Minitest.

bundle add rspec-rails rspec-wait factory_bot_rails ffaker vcr simplecov --group "development, test" && bundle add simplecov-rcov launchy --group "test" &&  
rails generate rspec:install && mkdir spec/system && 
git add . && git commit -m "adds rspec, factory boy, ffaker, vcr, simplecov, and launchy" && echo "\n.env\n.env.local\n.env.*.local\n\ncoverage/" >> .gitignore && git add . && git commit -m "adds .env, etc and coverage/ to .gitignore file" && sed -i '' -e 's/RSpec.configure do |config|/RSpec.configure do |config|\n  config.include FactoryBot::Syntax::Methods/g' spec/rails_helper.rb && sed -i '' -e "s/RSpec.configure do |config|/require 'simplecov'\nrequire 'simplecov-rcov'\nclass SimpleCov::Formatter::MergedFormatter\n  def format(result)\n    SimpleCov::Formatter::HTMLFormatter.new.format(result)\n    SimpleCov::Formatter::RcovFormatter.new.format(result)\n  end\nend\nSimpleCov.formatter = SimpleCov::Formatter::MergedFormatter\nSimpleCov.start 'rails' do\n  add_filter \"\/vendor\"\nend\n\nRSpec.configure do |config|/g" spec/rails_helper.rb && 
git add . && git commit -m "adding factorybot and simplecov to Rspec config"

3/ Debugging Tools

Adds Bullet and Active Record Query Trace as default debugging tools. They are enabled by default for development.

bundle add bullet active_record_query_trace  --group "development, test" && sed -i '' -e "s/Rails.application.configure do/Rails.application.configure do\n  config.after_initialize do\n    Bullet.enable        = true\n    Bullet.alert         = false\n    Bullet.bullet_logger = true\n    Bullet.console       = true\n    Bullet.rails_logger  = true\n    Bullet.add_footer    = true\n  end\n/g" config/environments/development.rb && echo "if Rails.env.development?\n  ActiveRecordQueryTrace.enabled = true\n  \nend" >> config/initializers/active_record_query_trace.rb && 
git add . && git commit -m "adding bullet, active_record_query_trace"

4/ Generate a Default Welcome Controller (optional)

Use this for quick demo apps that need a “home page.”

rails generate controller Welcome &&
sed -i '' -e 's/class WelcomeController < ApplicationController/class WelcomeController < ApplicationController\n  def index\n\n  end/g' app/controllers/welcome_controller.rb &&
echo "hello world" > app/views/welcome/index.erb &&
sed -i '' -e  's/# root "articles#index"//g' config/routes.rb && 
sed -i '' -e  's/Rails.application.routes.draw do/Rails.application.routes.draw do\n  root to: "welcome#index"/g' config/routes.rb && 
git add . && git commit -m "generates Welcome controller"

5/ CSS Options (pick one)

Rails 7 Add Bootstrap using old Boostrap gem for an IMPORTMAP APP

bundle add bootstrap && bundle install && ./bin/importmap pin bootstrap && ./bin/importmap pin @popperjs/core && 
sed -i '' -e 's/\/\/= link_directory ..\/stylesheets .css//g' app/assets/config/manifest.js && rm app/assets/stylesheets/application.css && echo "@import 'bootstrap/scss/bootstrap';\n@import 'bootstrap-icons/font/bootstrap-icons'; " > app/assets/stylesheets/application.scss && echo "import \"bootstrap\"" >> app/javascript/application.js && 
sed -i '' -e 's/# Rails.application.config.assets.precompile += \%w( admin.js admin.css )/Rails.application.config.assets.paths << Rails.root.join("node_modules\/bootstrap-icons\/font")/g' config/initializers/assets.rb  && git add . && git commit -m "bootstrap install using bootstrap gem"

Add Tailwind to Existing App

bundle add cssbundling-rails &&
./bin/rails css:install:tailwind &&
git add . && git commit -m "adds cssbundling-rails with tailwind" &&
rails generate controller Articles &&
echo '<div class="bg-blue-900 text-center py-4 lg:px-4"><div class="p-2 bg-blue-800 items-center text-blue-100 leading-none lg:rounded-full flex lg:inline-flex" role="alert"><span class="flex rounded-full bg-blue-500 uppercase font-bold px-2 py-1 text-xs mr-3">New</span><span class="font-semibold mr-2 text-left flex-auto">Hello Tailwind</span></div></div>' >> app/views/articles/index.html.erb
sed -i '' -e 's/# root "articles#index"/ root "articles#index"/g' config/routes.rb 
git add . && git commit -m "adds tailwind element" 

Rails 7 Bootstrap for ESBuild using sasss-css without CSSBundling (old-style)

(New apps should use CSSBundling-rails instead)

bundle add "sassc-rails" && yarn add @popperjs/core bootstrap bootstrap-icons sass &&
sed -i '' -e 's/"scripts": {/"scripts": {\n  "build:css": "sass .\/app\/assets\/stylesheets\/application.scss:.\/app\/assets\/builds\/application.css --no-source-map --load-path=node_modules",/g' package.json &&
echo "css: yarn build:css --watch" >> Procfile.dev &&
sed -i '' -e 's/\/\/= link_directory ..\/stylesheets .css//g' app/assets/config/manifest.js && rm app/assets/stylesheets/application.css && echo "@import 'bootstrap/scss/bootstrap';\n@import 'bootstrap-icons/font/bootstrap-icons'; " > app/assets/stylesheets/application.scss && 
echo "import * as bootstrap from \"bootstrap\"" >> app/javascript/application.js && 
sed -i '' -e 's/# Rails.application.config.assets.precompile += \%w( admin.js admin.css )/Rails.application.config.assets.paths << Rails.root.join("node_modules\/bootstrap-icons\/font")/g' config/initializers/assets.rb

Bootstrap, Font Awesome, and Kaminari (Rails 6 Old Style)

(New apps should use CSSBundling-rails instead)

bundle add bootstrap &&
echo "\n@import 'bootstrap';\n" >> app/assets/stylesheets/application.scss
sed -i '' -e  's/# Rails.application.config.assets.precompile += %w( admin.js admin.css )/Rails.application.config.assets.precompile += %w( application.scss )/g' config/initializers/assets.rb &&
sed -i '' -e 's/Rails.application.configure do/Rails.application.configure do\n  config.sass.inline_source_maps = true/g' config/environments/development.rb 
git add . && git commit -m "bootstrap install" &&

Old Style SASS Install Only

(For Rails 7, use CSSBundling-Rails instead.)

• Adds SASSC Rails & Renames Default application.scss to application.css

rm app/assets/stylesheets/application.css &&
echo "" > app/assets/stylesheets/application.scss && 
sed -i '' -e  's/# gem "sassc-rails"//g' Gemfile && sed -i '' -e 's/# Use Sass to process CSS//g' Gemfile && 
bundle install && bundle add sassc-rails && git add . && git commit -m "adds sassc-rails"

6/ Optional Styling Options

Font Awesome, Kaminari

bundle add font_awesome5_rails && 
echo "\n@import 'font_awesome5_webfont';\n" >> app/assets/stylesheets/application.scss
git add . && git commit -m "adds fontawesome" &&  
bundle add kaminari && 
rails generate kaminari:views bootstrap4 && 
git add . && git commit -m "adding kaminari and views"

7/ Hot Glue (pick one)

• Be sure to run Rspec install first (see section 2).

Add Hot Glue With Bootstrap

bundle add hot-glue && git add . && git commit -m "adds hot-glue" && 
rails generate hot_glue:install --layout=bootstrap
git add . && git commit -m "hot glue setup (bootstrap)"

Add Hot Glue with Tailwind

bundle add hot-glue && git add . && git commit -m "adds hot-glue" && 
rails generate hot_glue:install --layout=tailwind
git add . && git commit -m "hot glue setup (bootstrap)"

8/ Devise Setup For Rails 7

Devise has an annoying bug when used with Rails 7 Turbo. This fixes your views to bypass the Turbo interactions only for the view forms.

bundle add devise && bundle install && 
git add . && git commit -m "adding devise gem" && 
rails generate devise:install && 
git add . && git commit -m 'devise install' && 
rails g devise:views && 
sed -i '' -e  's/Rails.application.configure do/Rails.application.configure do\n  config.action_mailer.default_url_options = { host: "localhost", port: 3000 }/g' config/environments/development.rb && 
git add . && git commit -m 'devise setup' &&
sed -i '' -e 's/html: { method: :post }/html: { method: :post, 'data-turbo': false}/g' app/views/devise/confirmations/new.html.erb &&
sed -i '' -e 's/ url: session_path(resource_name))/ url: session_path(resource_name), html: {"data-turbo": false})/g' app/views/devise/sessions/new.html.erb && 
sed -i '' -e 's/ url: registration_path(resource_name))/ url: registration_path(resource_name), html: {"data-turbo": false})/g' app/views/devise/registrations/new.html.erb && 
sed -i '' -e 's/, html: { method: :post })/, html: { method: :post, "data-turbo": false })/g' app/views/devise/passwords/new.html.erb && 
git add . && git commit -m 'devise view fixes' 

9/ 12Factor, Nonschema Migrations (Heroku)

For all apps that you intend to deploy to a cloud environment. Not needed for demo apps or teaching apps that you will only run locally.

bundle add nonschema_migrations  && rails generate data_migrations:install && rails db:migrate && git add . && git commit -m "adds nonschema_migrations" && bundle add rails_12factor && echo "web: bundle exec rails server -p \$PORT\nrelease: ./release-tasks.sh" >> Procfile && echo "Step to execute\nbundle exec rails db:migrate data:migrate\n# check for a good exit\nif [ $? -ne 0 ]\nthen\n  puts '*** RELEASE COMMAND FAILED'\n  # something went wrong; convey that and exit\n  exit 1\nfi" >> .release-tasks.sh && chmod 0755 .release-tasks.sh && sed -i '' -e 's/# config.force_ssl = true/config.force_ssl = true/g' bundle install && bundle lock --add-platform x86_64-linux && git add . && git commit -m "setup for Heroku"

10/ Sidekiq, Sidekiq Scheduler

For all apps that will require jobs or scheduled jobs.

• Be sure to run the 12-Factor (Heroku) setup above first.

bundle add sidekiq sidekiq-scheduler && echo "require 'sidekiq/web'\nSidekiq::Web.set :session_secret, Rails.application.secret_key_base" >> config/initializers/sidekiq.rb && echo "worker: bundle exec sidekiq" >> Procfile && sed -i '' -e 's/Rails.application.routes.draw do/Rails.application.routes.draw do\n  mount Sidekiq::Web => "\/sidekiq" /g' config/routes.rb && echo ":schedule:" >> config/sidekiq.yml && git add . && git commit -m "adds sidekiq with configuration"