Create Your New Rails Project

Ahmed A.
4 min readJun 7, 2021

In this post I will show you how to set up a new ruby on rails project, rails is a web application framework that uses Ruby code to make your web application. It can handle user-side and server-side. It really is a good place for all in one web application, it’s so good that even Twitter used it in the early years. Rails offer conventions for handling routing, data, and much more base functionality for your web application. Rails also follow MVC (model-view-controller) design. Rails is an excellent choice for a small-scale web application that will use a database for storing information.

Requirements

Before we begin this tutorial you will need to make sure your machine has a few tools first. These tools include installing Ruby, Node, Yarn, and a database management system such as MySQL, PosgreSQL, or SQLite. You can see the setup with the official documentation here. This guide will be a summary of what you can read on the official docs. Another useful piece of knowledge is knowing how to use the command line in the terminal. (I will cover that in another post)

Making your new Rails project

To make a new project you will need to navigate to the folder where you want to store your project using the command line and then type the following command.

rails new my-rails-app -d postgresql

The command above creates a new Rails application called my-rails-app, you can of course call it whatever you would like but substituting the part of the command, next we see the -d which means that we want to create a database with the Rails application and in this example we chose PostgreSQL but you can change it to whatever type of database that you would like as long as it’s supported by rails. After doing that you will see a lot happening in the command line, just give it some time, and eventually you will see “Webpacker successfully installed”, that’s when you know we have created our Rails application. You can then cd into the created folder and then see the list of all the folders that are in the Rails app.

Database configuration

No matter what database you decide to use you will need to configure the database, we will do our example using PostgreSQL. Rails uses three databases; test, production, and development. With Rails version 6.0 one type of database is required from the start. We will look at how to setup all three databases by using the syntax below.

sudo -u postgres psql# This is how we sign into the database
create database rails_introduction_app_development;# Dev db
create database rails_introduction_app_production;# Production db
create database rails_introduction_app_test;# Test db

In the above example you can create the user for the test database, or you can do it in you text editor in the seed data folder which ever you prefer if you will create your test in the command line it would look something like the code below.

# Creating a new user, this will be used for Rails to gain access to each database
create user yournewuser with encrypted password 'yoursecretpassword';
# Grant priviledges for the newly created user to manage these databases
grant all privileges on database rails_introduction_app_development to yournewuser;
grant all privileges on database rails_introduction_app_production to yournewuser;
grant all privileges on database rails_introduction_app_test to yournewuser;

You can look in your db folder in VS code to see if they have been created or by typing the following into the command line.

\du
\l

In your text editor access the folder my-rails-app, and open the /config/database.yml. Here we are interested in the part below.

default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>development:
<<: *default
database: rails_introduction_app_developmenttest:
<<: *default
database: rails_introduction_app_testproduction:
<<: *default
database: rails_introduction_app_production
username: rails_introduction_app
password: <%= ENV['RAILS_INTRODUCTION_APP_DATABASE_PASSWORD'] %>

You may wonder why do you see three different database setups that is because we have set up three different databases of production, test, and development. As we see here, there are some defaults already setup for us. If you put your test data in the seed file you can skip this part. Here we can add our username and password

default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: username
password: password

Using your application

We can run the application by simply typing the below snippet into the command line.

rails server

You can also type “rails s” for short, this will start the webserver you will see some stuff pop up in the command line that will look like the following.

=> Booting Puma
=> Rails 6.0.3.2 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.5 (ruby 2.7.1-p83), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://127.0.0.1:3000
* Listening on tcp://[::1]:3000
Use Ctrl-C to stop

You can view the application webpage by typing http://localhost:3000 in the address bar and you will see the following.

Conclusion

Here we saw the setup for starting a new project with Ruby on Rails, from here you can start coding your application by setting up views and controllers which will add functionality to your application. In a later post I will cover how to make your web application functional. But here I have shown you the first step which is making the Rails app.

--

--