Create Your New Rails Project

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

Making your new Rails project

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

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

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store