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.
This and all future demos will assume you are starting in the workspace
directory.
1. Creating a New Rails Project
-
Check that RVM is set up to use the correct Ruby version by running the following command:
rvm list
The
=*
should appear next to version 2.6.5, indicating that it is both the current and default version of Ruby. -
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 (using the commandcode quiz-me
) and familiarize yourself with the Rails project directory structure. -
In the terminal, 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.5@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. -
The previous command also creates a backup file that looks similar to this:
.ruby-version.01.22.2020-09:48:07
. This file should be removed, because the colons in the filename can cause problems on some OSs. To remove the file, run this command:rm ./.ruby-version.*
-
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. Configuring PostgreSQL Databases 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. -
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:reset
It should complete without errors. Possible issues might be the postgresql service is not running, or the Postgres user role was not set up correctly.
3. Run the App
-
Start the development web server by running this command:
rails s
-
Open the web app in a browser by opening this URL: http://localhost:3000/
You should see a “Yay! You’re on Rails!” default page.
The basic Rails project skeleton is now up and running!
-
To halt the development web server, enter Ctrl-C in the terminal.