The Ruby on Rails framework emphasizes developer productivity, making it possible to implement sites that would once have taken months to build in a matter of weeks—or even days! Thanks to the Ruby programming language and principles such as convention over configuration and don’t repeat yourself, Rails developers spend less time configuring their applications and more time writing code.
Ruby on Rails is also a full-stack web framework, meaning it handles everything from accessing data in a database to rendering web pages in the browser. As a full-stack framework, Rails is made up of a seemingly endless list of different components, such as Active Record, the asset pipeline, CoffeeScript, Sass, jQuery, turbolinks, and a variety of testing frameworks.
This book aims to cut through that list and explain exactly what you need to know to develop your own Ruby on Rails applications. After you gain some experience with the fundamentals of Rails, I’ll introduce and explain new components of the framework as needed.
By the end, you’ll know how to build your own Rails application from scratch. You’ll add tests to ensure features work as expected, protect your application and your users from security vulnerabilities, optimize your application’s performance, and finally deploy your application to your own server.
I assume you have some experience with web development before starting this book. You
should be familiar with HTML and CSS. You should know what an H1
element
is and how to add images and links to a web page. Some knowledge of object-oriented
programming is helpful but not required.
You’ll use your computer’s terminal (or command prompt) to enter commands, but you don’t need much prior experience with terminal commands to follow the examples. In addition to the terminal, you’ll also need a text editor for writing Ruby code. Many Rails developers use a vintage editor, such as Vim or Emacs.
If you don’t already have a preferred text editor, I recommend Sublime Text. A free trial of Sublime Text is available online at http://www.sublimetext.com/. The free trial version never expires, but it does occasionally prompt you to purchase a license.
This book is divided into two parts. The first part covers the fundamentals of the Ruby language and the Ruby on Rails framework. The second covers advanced topics in both Ruby and Ruby on Rails. There are exercises at the end of every chapter, and solutions for them appear at the end of the book.
Chapter 1 covers the basics of Ruby, including datatypes, control flow, methods, and classes.
Chapter 2 covers the basics of Ruby on Rails. Topics include Rails principles, the directory structure used by Rails applications, and common Rails commands. You’ll create your first Rails application at the end of this chapter!
Chapter 3, Chapter 4, and Chapter 5 describe the three parts of the model-view-controller architecture used by Rails.
Chapter 6 covers creating a Git repository to store your application and deploying your application to the web using Heroku.
Once you understand the fundamentals of Ruby and Ruby on Rails, you’re ready for more advanced topics.
Chapter 7 covers Ruby modules, the Ruby object model, and even a bit of metaprogramming.
Chapter 8 covers more advanced Active Record associations. You’ll also build the data model for a new application at the end of this chapter.
Chapter 9 covers the authentication system used by your new application. This system allows users sign up for an account, log in to your application, and log off.
Chapter 10 covers automated testing for each part of your application using the MiniTest framework included with Ruby. This chapter also discusses test-driven development.
Chapter 11 covers common web application security vulnerabilities and explains how to make sure your application is secure.
Chapter 12 covers performance optimizations for Rails applications. Topics include the optimization features already built in to Rails, SQL query optimizations, and caching.
Chapter 13 explains several ways to track down bugs. Learn how to add to the log files generated by your application and how to use the interactive debugger for really tough bugs.
Chapter 14 explains how to use the GitHub API and then covers the process of creating your own API for your application.
Finally, Chapter 15 explains the process of setting up your own server on the Amazon cloud and deploying your application using Capistrano.
To follow the examples and complete the exercises in this book, you’ll need the Ruby programming language, the Ruby on Rails framework, the Git version control system, and the Heroku Toolbelt.
The Ruby language website provides installation instructions at https://www.ruby-lang.org/en/installation/. Rails is distributed as a collection of Ruby gems, which you’ll download and install with a single command that depends on your operating system. (The Ruby on Rails website also provides instructions at http://rubyonrails.org/download/.) You can download Git at http://git-scm.com/downloads/.
Once you’ve installed Ruby, Rails, and Git, install the latest version of the Heroku Toolbelt, which you’ll use to deploy your applications to Heroku. Download the Heroku Toolbelt installer from https://toolbelt.heroku.com/, and then follow the instructions there to complete the installation.
The sections below contain detailed installation instructions for Ruby, Rails, and Git on Mac OS X, Linux, and Windows. If you’re using Mac OS X or Linux, also see Multiple Ruby Versions for an alternative way to install Ruby. There’s a tool called pik for managing multiple Ruby versions on Windows, but it hasn’t been updated since 2012, so I won’t cover it here.
Check your current version of Ruby with ruby --version
. If you
have Mac OS X Mavericks, you should already have Ruby version 2.0.0. Otherwise, you need
to install a newer version.
Even if you already have Ruby 2.0.0, I recommend using the Homebrew package manager
on Mac OS X. Homebrew is an easy way to install and update common development tools on
Mac OS X. Instructions for downloading and installing Homebrew are online at
http://brew.sh/. Once you install Homebrew, open
a terminal and enter the command brew install
ruby
to install the latest version of Ruby.
Next, install Ruby on Rails with the command gem
install rails
. Then use Homebrew again to install Git by
entering the command brew install
git
.
Installation instructions for Linux differ slightly based on which Linux distribution you are using. First, check your package manager; it may have a recent version of Ruby. If so, just install that package as you would any other.
If not, you’ll need to install Ruby from source. Download the current stable version from https://www.ruby-lang.org/en/downloads/. Unpack the file and then enter the following commands in a terminal:
$ ./configure $ make $ sudo make install
Once the installation is complete, install Ruby on Rails by entering the command
sudo gem install rails
.
Every Linux distribution includes Git. Install Git with your package manager if it’s not already installed on your system.
You’ll use RubyInstaller to install Ruby. Download the RubyInstaller and the matching Development Kit from http://rubyinstaller.org/downloads/.
First, click the latest Ruby version on the RubyInstaller download page to download the installer; at the time of writing, it’s 2.1.5. Then scroll down to the section labeled Development Kit and click the link under your version of Ruby to download the Development Kit. As of this writing, for Ruby 2.1, you’d choose DevKit-mingw64-32-4.7.2-20130224-1151-sfx.exe. If you are using a 64-bit version of Windows, then download the 64-bit version of the installer and the matching 64-bit Development Kit, currently DevKit-mingw64-64-4.7.2-20130224-1151-sfx.exe.
Once these downloads finish, double-click the RubyInstaller file and then follow the prompts on your screen to install Ruby. Be sure to check the box next to Add Ruby executables to your PATH. Once that is complete, double-click the DevKit file and enter the path C:DevKit to extract the files. Now open a command prompt and enter the following commands to install the Development Kit:
$ cd C:DevKit $ ruby dk.rb init $ ruby dk.rb install
Some users see SSL errors when trying to install gems. Updating to the latest version of the gem
command corrects these errors. Enter the following command to update your version of gem
:
$ gem update --system –-clear-sources –-source http://rubygems.org
Once you’ve installed Ruby and the Development Kit, install Rails by entering gem install
rails
. This will connect to the RubyGems server, then download
and install the various packages that make up the Ruby on Rails framework.
Finally, download the latest version of Git and double-click the file to complete the installation.
Note: Throughout this book I ask you to enter commands such as bin/rake
and bin/rails
. These commands don’t work on Windows. Windows users, please add ruby
before these commands. For example, you will enter ruby bin/rake
and ruby bin/rails
.
Several third-party tools exist to make it easier to install and manage multiple versions of Ruby on a single computer. This can be useful if you maintain several different applications or if you want to test an application on a different version of Ruby.
The Ruby on Rails website recommends managing your Ruby installation with
rbenv
and the ruby-build
plugin. The
rbenv
command switches between Ruby versions and
ruby-build
provides the rbenv install
command that
you use to install different versions of Ruby.
If you’re using Mac OS X, both rbenv
and
ruby-build
can be installed using Homebrew. Instructions for
installing Homebrew are online at http://brew.sh/.
Open a Terminal, enter brew install rbenv
ruby-build
, and skip to Installing Ruby.
On Linux, install rbenv
and ruby-build
by
cloning the code from GitHub as shown below. Complete installation instructions are
available online at https://github.com/sstephenson/rbenv/.
First, make sure you have the proper development tools installed. The
ruby-build
wiki at https://github.com/sstephenson/ruby-build/wiki/ contains a
suggested build environment for most popular Linux distributions. For example, on
Ubuntu, enter the following command to install everything you need to compile
Ruby.
$ sudo apt-get install autoconf bison build-essential git libssl-dev libyaml-dev libreadline6 libreadline6-dev zlib1g zlib1g-dev Reading package lists... Done Building dependency tree --snip-- Do you want to continue? [Y/n]
Type the letter y
to install
these packages, and press ENTER. Packages needed for
other Linux distributions are listed on the wiki page above.
Next, enter the following command to clone the rbenv
git
repository into your home directory.
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv Cloning into '/home/ubuntu/.rbenv'... --snip-- Checking connectivity... done.
Then, add the ~/.rbenv/bin directory to your
$PATH
and add a line to your .bashrc file to
initialize rbenv
each time you log on.
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc $ echo 'eval "$(rbenv init -)"' >> ~/.bashrc $ source ~/.bashrc
Finally, install ruby-build
by cloning its git repository into
the rbenv
plugins directory with the following command.
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build Cloning into '/home/ubuntu/.rbenv/plugins/ruby-build'... --snip-- Checking connectivity... done.
Once you have rbenv
and ruby-build
installed,
you’re ready to install Ruby.
Enter the command rbenv install
-l
to list the currently available Ruby versions.
$ rbenv install -l Available versions: 1.8.6-p383 1.8.6-p420 1.8.7-p249 1.8.7-p302 --snip--
Ignore the versions with words such as jruby, rbx, and ree at the beginning. For now, just focus on the version numbers. The latest version as of this writing is 2.1.1. If there is a newer version when you install rbenv, replace 2.1.1 with the correct version number in the command below.
$ rbenv install 2.1.1 Downloading yaml-0.1.6.tar.gz... --snip-- Installed ruby-2.1.1 to /home/ubuntu/.rbenv/versions/2.1.1
Once this completes, enter rbenv global
2.1.1
to set your system’s global default Ruby version.
Now install Ruby on Rails by entering gem install
rails
. Finally, update rbenv
by entering
rbenv rehash
. You can learn more
about how rbenv
lets you switch Ruby versions at the
rbenv
website https://github.com/sstephenson/rbenv/.
52.15.55.18