Build a Simple Movie Reviews Website in Rails 6, part 1

A full website, from installation to deployment

By: Ajdin Imsirovic 14 December 2019

In this article, we’ll build a simple project in Rails 6. It will be a movie review website.

Build a simple movie review website in Rails 6 Image by CodingExercises

Prerequisites

For this project to be built as smoothly as possible, we will need to install Rails on Ubuntu 18.04 OS.

Thus, if you don’t already have Ubuntu 18.04 installed, you will need to do it first.

Once you have a brand new installation of Ubuntu 18.04, you can begin the installation of Ruby, RVM, and Rails. We’ll also need to install PostgreSQL and Yarn.

Ruby is the programming language that Rails is built in.

RVM stands for “Ruby Version Manager”, and it helps us install any version of Ruby on our machine easily. It also helps us switch between different Ruby versions just as easily.

Rails is the web framework we’ll be using in this tutorial.

PostgreSQL is our database engine.

Yarn is a JavaScript module manager, similar to npm.

Now that we’ve listed all the prerequisites for our project to be built, we’ll see how to install all of the above mentioned technologies on our Ubuntu 18.04 installation.

This process takes some time, and it’s not as easy or straightforward as you might want.

However, it’s a necessary obstacle to overcome, if you want to work with Rails.

Installing Rails on Ubuntu 18.04

Note: I have a 64-bit system with 16GB of RAM, with a 4-core i5 processor, and an

We’ll begin by installing Yarn.

Install Yarn

sudo apt-get install curl
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn

Next, we’ll install Ruby Version Manager.

Install RVM

To begin RVM installation, we need to install GPG keys, as explained at http://rvm.io/rvm/install.

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

RVM has a dedicated Ubuntu package. Instructions are available here: https://github.com/rvm/ubuntu_rvm.

sudo apt-get install software-properties-common
sudo apt-add-repository -y ppa:rael-gc/rvm
sudo apt-get update
sudo apt-get install rvm

Next, reboot your machine!

Intermission: Node.js

First, check the version of Node:

node -v

This currently returns:

v8.10.0

Now let’s check for npm:

npm -v

The terminal returns:

Command 'npm' not found, but can be installed with: sudo apt install npm

We’re going to take this approach, so:

sudo apt install npm

Sidenote: If you get an error like this:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 npm : Depends: node-gyp (>= 0.10.9) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

… you can try running this command:

sudo apt-get install nodejs-dev node-gyp libssl1.0-dev

The above command will ask you to confirm with a y, then produce a lot of output.

After this you can follow it up with:

sudo apt-get install npm

This time, you’ll need to confirm the installation with a y, and after a lot of output in the shell, you’ll get npm installation on your Ubuntu machine.

Now, let’s check again:

node -v && npm -v
> v8.10.0
> 3.5.2

Let’s update npm to latest version:

sudo npm install npm@latest -g

Now let’s see a list of npm modules installed. This command should return ‘empty’:

npm list

Let’s try the version again:

node -v && npm -v
> v8.10.0
> 3.5.2

Obviously, we need to restart the console (or we can run the source ~/.bashrc comand). Once we do, we can re-run the most recent command:

node -v && npm -v
> v8.10.0
> 6.13.1

Great, this took care of npm.

Next, let’s update our Node.js installation.

Install n package to update Node.js

To be able to update Node.js, we’ll install the n npm package:

sudo npm install -g n
> /usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
> n@6.1.2
> added 1 package from 4 contributors in 1.278s

Now we can update Node.js using n:

sudo n lts
>:
installing : node-v12.13.1
mkdir : /usr/local/n/versions/node/12.13.1
fetch : https://nodejs.org/dist/v12.13.1/node-v12.13.1-linux-x64.tar.gz
installed : v12.13.1 (with npm 6.12.1)

Note: the node command changed location and the old location may be remembered in your current shell.
old : /usr/bin/node
new : /usr/local/bin/node

We can reset the command location in two ways:

  1. Start a new shell, or
  2. execute PATH="$PATH"

So:

PATH="$PATH"
node -v
> v12.13.1

Let’s do it again for good measure:

node -v && npm -v
> v12.13.1
> 6.12.1

Now our “Node intermission” section of this setup is over, and we can continue with installing Ruby and making it work with Rails 6.

Install Ruby with RVM on Ubuntu 18.04

Let’s first see a list of known Ruby installations:

rvm list known

Now we can install version 2.6.2:

rvm install ruby-2.6.2
> Searching for binary rubies, this might take some time....

Now let’s look at the list of available installations:

rvm list

This returns:

Default ruby not set. Try 'rvm alias create default <ruby>'.

Googling for this issue retruns the following page: https://rvm.io/rubies/default.

If you run the suggested command:

rvm --default use 2.6.2

… you’ll get this error in the console:

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

To solve this issue, you need to change your terminal emulator preferences to allow login shell. Here’s the solution:

  1. In terminal, go to Edit > Preferences
  2. In the window that opens, under Profiles > Unnamed, clikc the Command tab
  3. Turn on Run the command as a login shell

Close and reopen the terminal.

Now we can run the command again:

rvm --default use 2.6.2

This will return:

=* ruby-2.6.2 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

Let’s see the available gems

Run this command in the shell:

gem list
>:
*** LOCAL GEMS ***

bigdecimal (default: 1.4.1)
bundler (default: 1.17.2)
bundler-unload (1.0.2)
cmath (default: 1.0.0)
csv (default: 3.0.4)
date (default: 2.0.0)
did_you_mean (1.3.0)
e2mmap (default: 0.1.0)
etc (default: 1.0.1)
executable-hooks (1.6.0)
fcntl (default: 1.0.0)
fiddle (default: 1.0.0)
fileutils (default: 1.1.0)
forwardable (default: 1.2.0)
gdbm (default: 2.0.0)
gem-wrappers (1.4.0)
io-console (default: 0.4.7)
ipaddr (default: 1.2.2)
irb (default: 1.0.0)
json (default: 2.1.0)
logger (default: 1.3.0)
matrix (default: 0.1.0)
minitest (5.11.3)
mutex_m (default: 0.1.0)
net-telnet (0.2.0)
openssl (default: 2.1.2)
ostruct (default: 0.1.0)
power_assert (1.1.3)
prime (default: 0.1.0)
psych (default: 3.1.0)
rake (12.3.2)
rdoc (default: 6.1.0)
rexml (default: 3.1.9)
rss (default: 0.2.7)
rubygems-bundler (1.4.5)
rvm (1.11.3.9)
scanf (default: 1.0.0)
sdbm (default: 1.0.0)
shell (default: 0.7)
stringio (default: 0.0.2)
strscan (default: 1.0.0)
sync (default: 0.5.0)
test-unit (3.2.9)
thwait (default: 0.1.0)
tracer (default: 0.1.0)
webrick (default: 1.4.2)
xmlrpc (0.3.0)
zlib (default: 1.0.0)
:<

Note the >: and <: “signs”. They’re just my way of showing the multi-line shell output’s starting and ending lines.

Next, let’s configure the .gemrc file to skip the installation of documentation:

echo "gem: --no-document" >> ~/.gemrc

Now whenever we run gem install <some gem>, the installation will be a lot faster because we won’t be pulling all the heavy documentation files for each gem.

Next, we’ll install git.

Install Git

With Ubuntu, it’s easy:

sudo apt install git

Now, let’s see the version:

git --version
> git version 2.17.1

Install Rails

Let’s reiterate:

rvm list
> =* ruby-2.6.2 (current && default)

Let’s check the version:

ruby -v
> ruby 2.6.2p47 (2019-03-13 revision 67232) ...

Rails is just a gem. So we can install different versions of Rails with gem install command.

Let’s see all the avilable versions:

gem search '^rails$' --all

>:
rails (6.0.1, 6.0.0, 5.2.3, 5.2.2.1, etc...)

Next, we can install the version we want. Before we do, let’s make sure what installations are already present:

rails -v
> Command 'rails not found, but can be installed with: sudo apt install ruby-railties

We’ll skip this suggestion, and install it with gem install instead:

gem install rails -v 6.0.0.

This is an interesting place to mention gemsets. You can read up on gemsets here.

I’ve verified the installed gems earlier, so gem install bundler is not necessary as it already exists in my gem list command.

Let’s now configure our git account.

Configure Git

Here’s the list of commands to run:

git config --global color.ui true
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR@EMAIL.com"
ssh-keygen -t rsa -b 4096 -C "YOUR@EMAIL.com"

We need to use the newly-generated SSH key and add it to our Github account.

To do that, we need to run the following command:

cat ~/.ssh/id_rsa.pub

We need to copy the output of the above command and paste it here: https://github.com/settings/ssh or here: https://github.com/settings/keys. Note that it seems that the url ending with ssh was the old url (still seems to work in 2020), and the url ending with keys is the new url.

Now let’s verify that it worked:

ssh -T git@github.com

You’ll get the following notification:

The authenticity of host 'github.com (--------------)' can't be established.
RSA key fingerprint is SHA256:--------------------------------.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added 'github.com,----------------' (RSA) to the list of known hosts.

Obviously, the above hyphens replace the actual URL and hash values.

After confirming with a yes, this is the desired output:

Warning: Permanently added 'github.com,-------------------' (RSA) to the list of known hosts.
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

Setting Up PostgreSQL

Let’s try to install Postgres; this will FAIL:

sudo apt install postgresql-11 libpq-dev

Instead, let’s do this:

sudo apt update
sudo apt install postgresql postgresql-contrib

Using PostgreSQL Roles and Databases

To take care of authentication and authorization, Postgres uses roles.

Postgres uses ident authentication. It matches Postgres roles with Linux system account.

Thus, for each role in Postgres, if there’s the same username in Linux, you’ll be able to sign in with that role.

During Postgres installation, a user account is made. This user account is called simply postgres. It is the default role.

That’s the account you need to log into to use Postgres.

Logging Into the postgres Account

To log into default postgres account:

$ sudo -i -u postgres

Now that you’ve switched into postgres account, you can go into Postgres prompt with:

psql

Now you’re inside the PosgreSQL database management console.

To quit, type:

\q

Adding a New Rails Project with a PostgreSQL Database

Let’s try running rails new to add a new app.

Note: this will fail.

We’ll pass it the -d flag to specify the database:

rails new myapp -d postgresql

Instead of a new Rails installation, we’ll see an error.

Here’s the error that gets output.

Results logged to
/home/pc/.rvm/gems/ruby-2.6.2/extensions/x86_64-linux/2.6.0/pg-1.1.4/gem_make.out

An error occurred while installing pg (1.1.4), and Bundler cannot
continue.
Make sure that `gem install pg -v '1.1.4' --source 'https://rubygems.org/'`
succeeds before bundling.

In Gemfile:
    pg
        run  bundle binstubs bundler
Could not find gem 'pg (>= 0.18, < 2.0)' in any of the gem sources listed in
your Gemfile.
        run  bundle exec spring binstub --all
bundler: command not found: spring
Install missing gem executables with `bundle install`
        rails  webpacker:install
Could not find gem 'pg (>= 0.18, < 2.0)' in any of the gem sources listed in your Gemfile.
Run `bundle install` to install missing gems.

I’ve found the solution for this on Stackoverflow.

sudo apt install postgresql-contrib libpq-dev

Now it works.

Next, before we actually add the myapp app, let’s add a new role, the myapp role.

$ sudo su - postgres psql
> [sudo] password for <linux-username>: ***
$ postgres@<linux-username>:~$ psql -U postgres
> psql (10.10 (Ubuntu 10.10-0ubuntu0.18.04.1))
Type "help" for help.

postgres=# create role myapp with createdb login password 'password1';

Note: do not forget the ; at the end of the above line!

If your command was successfully executed, you’ll get the following output in the console:

postgres=# create role myapp with createdb login password 'password1';
CREATE ROLE
postgres=# 

If the above doesn’t work, try this:

$ sudo - u postgres psql
# if prompted, type your Linux password for sudo operations
psql (12.6 (Ubutnu 12.6-@ubuntu0.20.04.1))
Type "help" for help.

postgres=# create user pc with password '12345678';
CREATE ROLE
postgres=# alter user pc with superuser;
ALTER ROLE
postgres=# \q

Explanation of what’s happenning above:

  • first we log into postgres in console
  • then we create a user named pc (for example), with a password
  • because Rails will use the above user we’ll give this user some superpowers with alter user pc with superuser;, which will return ALTER ROLE confirmation.

Let’s now list our postgres users:

postgres=# \du

Here’s the list of roles that gets returned:

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 myapp     | Create DB                                                  | {}
 pc        | Superuser                                                  | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

Once we’ve verified we’ve added our user we can run the rails new command.

Add a New Rails Project (Successfully)

Adding a new Rails project is easy now:

rails new myapp -d postgresql

Next:

cd myapp

Now:

rails db:create
> Created database '<linux-user>_development'
> Created database '<linux-user>test'

Finally:

rails s

The rails s command is short for rails server. This will run our project, and we can see it at http://localhost:3000.

Now that we’ve successfully installed and setup all the prerequisites, we can start working on our project, in part 2.

Feel free to check out my work here: