Intro

In this series of tutorials, we will go through how to build a Rails application with authentication/authorization. We will briefly go over what each function does and set up a bare-bones application template to use with React.js.This particular article will focus on just setting up the environment so that we can start the project.

To make sure you are on the same page, you can start on a brand new virtual machine running Ubuntu 18.04. You can download virtual machines from here: 

https://www.vmware.com/ca.html for VMware (free for Windows) or https://www.virtualbox.org/ for VirtualBox (free for MacOS and Windows). 

Ubuntu Desktop operating system can be download here: https://www.ubuntu.com/download or if 18.04 is not the latest version anymore, you can also check here: http://old-releases.ubuntu.com/releases/

Start

To start, we will install the dependencies needed to install Ruby on Rails: 

Open up the terminal by pressing ctrl + alt + T and type in the following to start installing (make sure you wait for each command to finish before running the next): 

Install Git for version control

sudo apt install git

Install libraries and other dependencies

sudo apt update
sudo apt install curl
sudo apt install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm-dev

Installs RVM for managing Ruby environments

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

Note:

If at any point the terminal says you have not installed or function is not found when you have just installed the program succesfully, an easy workaround is just close the terminal and open it again. You can also type source ~/.bashrc or source ~/.bash_profile to see if it works. If it still doesn’t work, make sure the program was properly installed and there were no error messages in the logs. 

Continue setting up ruby environment

rbenv install 2.5.1
rbenv global 2.5.1
echo "gem: --no-document" > ~/.gemrc
gem install rails -v 5.2.0
rbenv rehash

Install NodeJS (JavaScript Runtime environment)

cd /tmp
\curl -sSL https://deb.nodesource.com/setup_6.x -o nodejs.sh
cat /tmp/nodejs.sh | sudo -E bash -
sudo apt install -y nodejs

Cleanup

Almost done the setup, let’s do some cleaning up and updating just to be sure

cd ~/.rbenv
git pull
cd ~
sudo apt install postgresql postgresql-contrib libpq-dev
sudo apt update

Now we should be ready to begin creating the app.

Creating The App

Create the application

rails new rails_template
cd rails_template

Make sure you have installed a text editor so that we can edit some files For Sublime text install, do: 

wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add -
echo "deb https://download.sublimetext.com/ apt/stable/" | sudo tee /etc/apt/sources.list.d/sublime-text.list
sudo apt-get update
sudo apt-get install sublime-text

For Vim install, do: 

sudo apt install vim

Gemfile

Then open up the Gemfile edit it to include the proper gems. In particular, we are going to need Devise for authentication and CanCanCan for authorization. Authentication would mean the parts responsible for logging in a user, whereas authorization would be permissions for which types of users (roles) are allowed to do certain actions. These are usually very important for people to define in projects, since you wouldn’t want a guest to be able to access Admin level functions (such as deleting users). 

The Gemfile should look like this: https://gist.github.com/mt9304/78c1ab6274d92629758b05c258def17e

Note: 

If you are unfamiliar with gems, you can think of them as additional programs, plugins/extensions, or libraries that the application can use.

Turbo Links

We also need to remove Turbo Links since it can cause issues with some of the other technologies we are going to use later on. Remove the “//= require turbolinks” line from app/assets/javascripts/application.js. Remove the two “data-turbolinks-track” => true hash key/value pairs from your app/views/layouts/application.html.erb.

The application.html.erb file should look like this: https://gist.github.com/mt9304/6028e18397f0af7748d3df76fe4b9292

Note: 

The application.html.erb file here determines how your pages should look. In general, all your pages in this application will use the HTML here for its layout, and the <%= yield %> tag is the part that changes depending on the page.

Database

After this, let’s run the following to install our database

sudo apt install libsqlite3-dev
sudo apt install sqlite3

We will change to using Postgres database later

Note:

Sqlite is a lightweight database good for development, but not that great at handling a lot of connections. So we will use Postgresql later on when we move it into production. 

Now that we have saved the new Gemfile, run the following command inside the application’s directory to install the gems: 

bundle install

React

Let’s generate the files for React so we can use them later on: 

rails g react:install

After everything has been installs, run the following to start the server, then visit localhost:3000 to see if the application started successfully. 

rails s

If all is successful so far, it should say Yay! you’re on Rails!

To stop the application server from running, press CTRL+ C at the terminal to stop the process. 

Now that the application is up and running, we can start making a basic API.

Navigation

The next article can be found here.