· Ruby · 3 min read
How to create tons of Rails apps
Yes, I already now about the rails new command. But what if you want better defaults, like an existing controller, an existing view, or another way to to launch the Procfile?
1. Motivation
As mentionned, there is already the official way, that I briefly summarized in a article about rails new option.
But it’s not enough for my taste.
In order to
- grab any concept,
- isolate a bug quickly,
- play with new Rails features,
I need at least
- A route,
- A view,
- To “cd” in the repo
- To init the repo
- and now a different way to handle bin/dev (from Matt Brictson)
These are tiny steps, but I create new apps so often, that the official way is not good enough for my taste.
But maybe tomorrow I’ll need different things. I don’t want to git init the repo
Side note for beginners : It’s also an excellent way to improve your Ruby-on-Rails skills. The more you create new apps from scratch, the more you understand the directory structure, the philosophy, and the internals of Rails.
2. My trick
Open ~.bash_profile
on your linux-based computer.
We will create a bash function, named cnra (an acronym that means “create new rails application”). Acronyms are very handy to avoid remembering every shortcut.
# inside ~.bash_profile
#
# Example usage :
# $> cnra myapp 8.0.0 --minimal --database=postgresql
#
cnra ()
{
# create dir, dive into dir, require desired Rails version
mkdir -p -- "$1" && cd -P -- "$1"
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '$2'" >> Gemfile
# install rails, create new rails app
bundle install
bundle exec rails new . --force ${@:3:99}
bundle update
# Create a default controller
echo "class HomeController < ApplicationController" > app/controllers/home_controller.rb
echo "end" >> app/controllers/home_controller.rb
# Create a default route
echo "Rails.application.routes.draw do" > config/routes.rb
echo ' get "home/index"' >> config/routes.rb
echo ' root to: "home#index"' >> config/routes.rb
echo 'end' >> config/routes.rb
# Create a default view
mkdir app/views/home
echo '<h1>This is h1 title</h1>' > app/views/home/index.html.erb
# Create database and schema.rb
bin/rails db:create
bin/rails db:migrate
}
Note the trick ${@:3:99}
that means “all remaining CLI args, from the 3rd one to the last one.”
Otherwise, comments should be self-explanatory.
Now type
$> source ~/.bash_profile
So that your terminal will be aware of what changed inside your .bash_profile
.
3. Usage
Open your terminal
$> cnra myapp 8.0.0 --minimal -d=postgresql
1st CLI arg is “myapp” : the name of the new app 2nd CLI arg is 8.0.0 : the version of Rails you want to try 3rd CLI arg is —minimal -d=postgresql : PostGre is a production-ready database you can easily use locally.
Note that --minimal
and -d=postgresql
are optionals.
4. Going further
We personally like the minimal flag (to avoid all the default gems we probably won’t need), and the “postgresql” database - a production-ready, widely used database in the Ruby-on-Rails world. So we use another shortcut, based on the previous one.
cnra8mp() {
cnra myapp 8.0.0 --minimal -d=postgresql
}
All we have to do now each time we want to try something with Rails is the following :
$/workspace> cnra8mp
$/workspace/myapp> bin/rails server
And voilà ! A new Rails application up and running, no need to create the database, or build a default welcome page : our app is ready for experimentations.
Summary
Nothing fancy, but a simple help to re-start quickly from scratch.