Blog posts

Home > Blog > Set Up Ruby On Rails 5
Set Up Ruby On Rails 5

Set Up Ruby On Rails 5

Installing Homebrew

Homebrew allows you to install and compile software packages easily from source. It comes with a very simple install script. Follow all the steps and when macOS asks you to install XCode CommandLine Tools, say Yes.

Installing Ruby

Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. You should install the version 2.5.1. After all, as you will use rbenv to install and manage it, it'll be really easy to upgrade in a newer version when you have to. So far, so good ! Let's do it !

# brew install rbenv ruby-build

// Add rbenv to bash so that it loads every time you open a terminal
# echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' » ~/.bash_profile
# source ~/.bash_profile

// Install Ruby
# rbenv install 2.5.1
# rbenv global 2.5.1
# ruby -v

Installing Rails

Rails is a Model–View–Controller (MVC) framework, providing default structures for a database, a web service, and web pages. It encourages and facilitates the use of web standards such as JSON for data transfer, and HTML, CSS and JavaScript for display and user interfacing.

In addition to MVC, Rails emphasizes the use of other well-known software engineering patterns and paradigms, including Convention over Configuration (CoC), Don't Repeat Yourself (DRY), and the Active Record Pattern.

Installing Rails is as simple as running the following command in your Terminal:

# gem install rails -v 5.2.0

Rails is now installed, but in order for you to use the rails executable, you need to tell rbenv to see it. This command is really useful when you have to install new gem component:

# rbenv rehash

And now, you can verify Rails is well installed:

# rails -v
# Rails 5.2.0

Installing MySQL

MySQL is an open-source relational database management system. Like Ruby you can install MySQL server and client from Homebrew:

# brew install mysql

Once this command is finished, it gives you a couple commands to run. Follow the instructions and run them:

# mysql.server start
# mysql_secure_installation

Create a new Rails application

Rails comes with a number of scripts called generators that are designed to make your development life easier by creating everything that's necessary to start working on a particular task. One of these is the new application generator.

Let's create a new Rails application with new generator to verify if all is up and running:

# rails new myapp -d mysql

Add your Rails app to a Git repository

Create a repository on Bitbuchet and push your Rails app on it.

# git remote add origin ssh://git@bitbucket.org/you_user_id/repository_name.git
# git push -u origin master

Final Steps

Slim is a templating system to avoid writing the inline code in a web document and make HTML easy and clean. Slim gives the flexibility to have some dynamic content in HTML. Add Slim to your project Gems. Open myapp/Gemfile.

Use Puma as the app server gem 'puma', '˜> 3.0'
Use slim gem 'slim'
Use SCSS for stylesheets gem 'sass-rails', '˜> 5.0'

Update the Rails application gem's:

# cd myapp
# bundle install

Modify the myapp/config/database.yml file to contain the username & password that you specified:

default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: username
password: password
socket: /tmp/mysql.sock

Create the database:

# cd myapp
# rake db:create

Lunch Rails application:

# cd myapp
# rails server

You can now visit http://localhost:3000 to view your brand new Rails application!

Create the very first Controller

To create a new controller, you need to run the controller generator and tell it you want a controller called Home with an action called index, just like this:

# cd myapp
# rails generate controller Home index

Rails create several files and a route for you.

create app/controllers/home_controller.rb
route get 'home/index'
invoke erb
create app/views/home
create app/views/home/index.html.erb
invoke test_unit
create test/controllers/home_controller_test.rb
invoke helper
create app/helpers/home_helper.rb
invoke assets
invoke coffee
create app/assets/javascripts/home.coffee
invoke scss
create app/assets/stylesheets/home.scss

Rename the app/views/home/index.html.erb file to app/views/home/index.html.slim. Open it in your text editor. Delete all of the existing code in the file, and replace it with the following single line of code:

h1 Homepage

Visit http://localhost:3000/home/index to view your page.

Setting your Rails app Home Page

Now that you have made the controller and view, you need to map this page to the root of the application. Edit the myapp/config/routes.rb file in your editor and add the line of code root 'home#index':

Rails.application.routes.draw do
    root 'home#index', as: 'home_index'
end

Go to http://localhost:3000/, your home page is on! Well Done!

about me

Laurent Raboteau

Music lover, assiduous reader, surfer & snowboarder, I live staring at the Atlantic Ocean and below Pyrénées with my lovely wife and childrens. During week, I work for Salesforce

Categories