Demo 2: Setting Up a New Rails Project
In this demonstration, I will show you how to create and setup a new Rails 6 project. The application I will create in these tutorials is QuizMe, a quizzing application similar to Quizlet, but with multiple choice and fill in the blank questions.
This and all future demos will assume you are starting in the workspace
folder on your Linux VM.
1. Creating a New Rails Project
-
Set up RVM to use Ruby version 2.6.3 as the default version by running the following command:
rvm use ruby-2.6.3 --default
You can check that this has worked correctly by running the following command:
rvm list
Verify that
=*
appears next to version 2.6.3, indicating that it is both the current and default version of Ruby. -
Install the Rails 6 gem using the following command:
gem install rails
Verify that the correct version was installed by running the following command:
rails -v
Verify that the output is
Rails 6.0.0
. -
Create a new Rails project backed by a PostgreSQL database by entering the following command:
rails new quiz-me --database=postgresql --skip-coffee
More details about this command…▼
By Rails convention the name of the project should be the name of the app in lowercase with words separated by dashes. SQLite is the default database for Rails Applications so the
--database
option is required to use PostgreSQL. Rails applications also default to automatically creating Coffeescript files, but the--skip-coffee
option specifies plain Javascript files should be created instead.This command will take a while to complete and run several intermediate commands to complete the process. More details…▼
The project directory files are created first. Then, new gems are installed by running
bundle install
. Beware, some gems can take a long time to install (nokogiri, pg), but this is normal. One of the gems installed is webpacker which allows the app to serve all the project's JavaScript files in one large file, but it requires some installation setup which is the next thing to run asrails webpacker:install
. Last the node packages will be installed by runningyarn install
. Many packages and their dependencies should be added. There will likely be a few dependency warnings which will later be resolved, but there should be no symlink errors. -
Open the
quiz-me
project folder in VS Code and familiarize yourself with the directory structure. -
Change directory into the
quiz-me
folder (cd quiz-me
), and create a new project-specific gemset by running the following command:rvm use ruby-2.6.3@quiz-me --ruby-version --create
This command will create two files,
.ruby-gemset
and.ruby-version
, if they do not already exist to store the Ruby version and gemset information for RVM. -
Open the file
Gemfile
in the top level of thequiz-me
folder. This file declares all the gems required for the project. Another package is needed to ensure that PosgreSQL works correctly as the project’s database backend. To add the package insert the following code lines at the end of the file:# Disconnects all connections to PostgreSQL db when running rails db:reset gem 'pgreset', '~> 0.1.1'
Without the
pgreset
gem, you will getPG::ObjectInUse
errors when attempting to drop the database while pgAdmin is running. -
Install all the gems into the new gemset by running the following command:
bundle install
2. Creating a PostgreSQL Database for the New Rails Project
-
Open the file
config/database.yml
. This file contains the connection information for the project’s three PostgreSQL databases. Rails uses three environments (development, test, and production), each with their own separate databases. -
Create the three PostgreSQL databases with the names specified in the
database.yml
file by running the following commands:sudo -i -u postgres psql -c "CREATE DATABASE quiz_me_development OWNER vagrant;" sudo -i -u postgres psql -c "CREATE DATABASE quiz_me_test OWNER vagrant;" sudo -i -u postgres psql -c "CREATE DATABASE quiz_me_production OWNER vagrant;"
The above commands can be adapted for any project by following the below template and changing the values of the variable
PG_DB_ROOT
with the appropriate information for the project. The first two psql commands only need to be run once on a system:PG_USER="vagrant"
PG_PSWD="password1"
PG_DB_ROOT="default"
sudo -i -u postgres psql -c "CREATE USER $PG_USER WITH PASSWORD '$PG_PSWD';"
sudo -i -u postgres psql -c "ALTER USER $PG_USER WITH SUPERUSER CREATEDB;"
sudo -i -u postgres psql -c "CREATE DATABASE ${PG_DB_ROOT}_development OWNER $PG_USER;"
sudo -i -u postgres psql -c "CREATE DATABASE ${PG_DB_ROOT}_test OWNER $PG_USER;"
sudo -i -u postgres psql -c "CREATE DATABASE ${PG_DB_ROOT}_production OWNER $PG_USER;"
- Make the following changes to the
database.yml
file:-
Add username and password to the default configuration to match the following:
default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: vagrant password: password1
-
Comment out the preset username and password on the production database to match the following:
production: <<: *default database: quiz_me_production # username: quiz_me # password: <%= ENV['QUIZ_ME_DATABASE_PASSWORD'] %>
-
-
Confirm the database configuration is correct by running the following commands:
rails db:migrate rails db:reset