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
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
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
# rbenv rehash
And now, you can verify Rails is well installed:
# rails -v
# Rails 5.2.0
# 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
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://firstname.lastname@example.org/you_user_id/repository_name.git
# git push -u origin master
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
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
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.
route get 'home/index'
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:
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
Rails.application.routes.draw do root 'home#index', as: 'home_index' end
Go to http://localhost:3000/, your home page is on! Well Done!