/ Ruby on Rails

Ruby On Rails Setup

This is the first in a series of tutorials on Ruby on Rails where we will be building a client-manager app.

This tutorial will go through setting up Ruby on Rails on a Mac (Sierra 10.12). If you are on another OS the only Mac specific portion of this tutorial is setting up rbenv with homebrew. Everything else should be OS agnostic.

We will be using the following ruby and gems.

  • Ruby 2.4
  • Rails 5.0.2
  • RSpec 3.5
  • FactoryGirl 4.8
  • bootstrap-sass 3.3.7
  • Capybara 2.12
  • Selenium-webdriver 3.3
  • Database cleaner 1.5
  • PostgreSQL 9.5


To install ruby on a Mac we will be using rbenv. The easiest way to get up and running with rbenv is with homebrew. Homebrew is a package manager for the MacOS.

To install homebrew running the following command in your terminal.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Once homebrew is installed run the following command.

brew update

This command will update homebrew. You will always want to run this before you install or upgrade a formula(package).


[Rbenv](https://github.com/rbenv/rbenv) is a ruby package manager that allows you to install and manage multiple versions of ruby easily.
brew install rbenv ruby-build

ruby-build is a plugin for rbenv that provides a install command to compile and install different versions of ruby.

The final step to install rbenv is to run the init.

rbenv init

This sets up a few commands in your shell.


To view all the available ruby versions that you can install run the following.

rbenv install -l

Periodically you will need to update ruby-build when new versions of ruby are released.

brew update
brew upgrade ruby-build

To install ruby 2.4.

rbenv install 2.4.0

This will download and compile the ruby version specified.

Once that's complete run.

rbenv global 2.4.0

This will set ruby 2.4 as our main ruby for our system.

To verify this run.

ruby -v

You should see something similar too.

ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]

Now with ruby installed lets upgrade our gem to the latest version.

gem update --system

And lets install bundler.

gem install bundler


For development I always use the same database that I will be using for production which is PostgreSQL. But for this tutorial you can always use what you prefer. Rails by default uses SQLite.

But if you do prefer to use PostgreSQL follow the method to install for your OS.

For Mac you can always install using Homebrew

brew install postgresql

But I prefer using the Postgres.app to install PostgreSQL in a self contained environment.

Ruby on Rails

With ruby now setup lets install Ruby on Rails.

gem install rails -v 5.0.2

Lets verify rails is installed.

rails -v
#Rails 5.0.2

Now we can create our rails project.

rails new client-manager -T --database=postgresql

The -T argument specifies that we will be skipping using minitest and database=postgresql specifies that we will be using PostgreSQL for our database.

If you don't have PostgreSQL installed you can just skip using that argument and rails will use SQLite as the default database.

Now we just have to create our development and test database.

bin/rails db:create

Lets run our rails site by starting the server.

bin/rails server

Once the server is up and running open your bower and visit http://localhost:3000/. You should see the default rails welcome page.

Welcome Page

Lets install some gems.


Open the `Gemfile` which is in the root of our rails site and add the following.
gem 'bootstrap-sass', '~> 3.3.7'

Now lets install the gem.

bundle install

Once installed rename app/assets/stylesheets/application.css to app/assets/stylesheets/application.scss

Open app/assets/stylesheets/application.scss and remove everything including the require tags and add the following to the top of the file.

@import "bootstrap-sprockets";
@import "bootstrap";

Open app/assets/stylesheets/application.js and add the following above the //= require turbolinks.

//= require bootstrap-sprockets


Rails by default comes installed with minitest but we opt out when we installed our rails site. We will be using [RSpec](http://rspec.info/) for our tests.

Add the rspec gem to :development and :test group in your Gemfile

group :development, :test do
  gem 'rspec-rails', '~> 3.5'

Lets install the gem.

bundle install

We now need to initialize RSpec.

bin/rails generate rspec:install

This will setup rspec to work with rails.


Capybara is a web based testing framework that allows you to simulate how a user would interact with your site.

Add the capybara gem to the :test group in your Gemfile.

group :test do
  gem 'capybara', '~> 2.12'

Lets now install the gem.

bundle install

Open spec/rails_helper.rb and add the following right below require 'rspec/rails'

require 'capybara/rspec'

Factory Girl

Factory girl is a replacement to using fixtures. Which allows you to define a factory(model) when testing.

Add the factory girl gem to the :development and :test group in your Gemfile

group :development, :test do
  gem "factory_girl_rails", "~> 4.8"

Lets now install the gem.

bundle install

Create a new directory under the spec folder called support and add a new file called factory_girl.rb. Add the following to the new file.

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods

Open spec/rails_helper.rb and the following under require 'capybara/rspec'.

require 'support/factory_girl'

Selenium Webdriver

Capybara uses the selenium webdriver to allow you to run tests that rely on javascript through a web browser.

Add the selenium-webdriver gem to the :test group in you Gemfile.

group :test do
  gem 'selenium-webdriver', '~> 3.3'

Lets now install the gem.

bundle install

By default selenium uses firefox so you may need to install the latest gecko drivers for your OS.

But for Mac you can just use homebrew.

brew install geckodriver

Database Cleaner

By default tests when interacting with the database are ran in a transaction. So changes are always rolled back. But the process that allows us to test javascript does not work within a transaction. So we have to use the database cleaner gem to allow us to specify that javascript tests use truncation instead.

Add the database cleaner to the :test group in your Gemfile.

group :test do
  gem 'database_cleaner', '~> 1.5'

Lets now install the gem.

bundle install

Open spec/rails_helpr.rb and change the line

config.use_transactional_fixtures = true


config.use_transactional_fixtures = false

Create a new file at spec/support/database_cleaner.rb with the following

RSpec.configure do |config|
  config.before(:suite) do
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  config.before(:each) do
  config.after(:each) do

Open spec/rails_helper and add the following after require 'capybara/rspec'

require 'support/database_cleaner'

Our rails environment should now be setup. You can view the repo for this tutorial on Github

In the next tutorial will start adding features using TDD(Test driven development).