Learn Rails 2
Learn Rails 2
Daniel Kehoe
ii
Contents
1 Free Offer
2 Introduction
Is It for You? . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What To Expect . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Staying In Touch . . . . . . . . . . . . . . . . . . . . . . . .
Example Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
iii
CONTENTS
iv
Version Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
11
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
RailsGuides . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
Cheatsheets . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
API Documentation . . . . . . . . . . . . . . . . . . . . . . .
13
13
Pair Programming . . . . . . . . . . . . . . . . . . . . . . . . . . .
14
15
Code Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
Staying Up-to-Date . . . . . . . . . . . . . . . . . . . . . . . . . .
16
19
GitHub . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
20
MailChimp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
SendGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
Heroku . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
5 Get Started
23
23
24
Your Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . .
25
Hosted Computing . . . . . . . . . . . . . . . . . . . . . . .
26
26
CONTENTS
Installing Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . .
28
Ubuntu Linux . . . . . . . . . . . . . . . . . . . . . . . . . .
28
Hosted Computing . . . . . . . . . . . . . . . . . . . . . . .
28
Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
Your Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
30
31
RVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
Project-Specific Gemset . . . . . . . . . . . . . . . . . . . . . . . .
34
37
Starter Applications . . . . . . . . . . . . . . . . . . . . . . . . . .
37
38
39
40
43
43
44
44
45
45
46
46
CONTENTS
vi
7
49
Project Directory . . . . . . . . . . . . . . . . . . . . . . . . . . .
50
51
51
52
52
52
54
57
Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
58
Is Git Installed? . . . . . . . . . . . . . . . . . . . . . . . . . . . .
60
Is Git Configured? . . . . . . . . . . . . . . . . . . . . . . . . . . .
60
Create a Repository . . . . . . . . . . . . . . . . . . . . . . . . . .
61
GitIgnore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
62
Git Workflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
Untracked Files . . . . . . . . . . . . . . . . . . . . . . . . .
63
Staging . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
64
Committing . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
Git Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
Pushing to GitHub . . . . . . . . . . . . . . . . . . . . . . .
67
The README . . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
9 Gems
Rails Gems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
71
72
CONTENTS
vii
73
74
Gemfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
76
77
Adding Gems . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
80
82
82
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . .
84
Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
84
10 Configure
87
Configuration Security . . . . . . . . . . . . . . . . . . . . . . . .
87
88
89
SendGrid . . . . . . . . . . . . . . . . . . . . . . . . . . . .
90
MailChimp . . . . . . . . . . . . . . . . . . . . . . . . . . .
90
Owner Email . . . . . . . . . . . . . . . . . . . . . . . . . .
91
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . .
91
92
Domain Name . . . . . . . . . . . . . . . . . . . . . . . . . .
93
93
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . .
94
95
Configure Email . . . . . . . . . . . . . . . . . . . . . . . . . . . .
96
CONTENTS
viii
96
98
Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
98
101
105
117
CONTENTS
ix
Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Scaffolding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Test the Application . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
14 Troubleshoot
133
Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Interactive Ruby Shell . . . . . . . . . . . . . . . . . . . . . . . . . 134
IRB for Blocks of Code . . . . . . . . . . . . . . . . . . . . . 135
Quitting IRB . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Learn More About IRB . . . . . . . . . . . . . . . . . . . . . 136
Beyond IRB . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Rails Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Rails Logger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Revisiting the Request-Response Cycle . . . . . . . . . . . . . . . 141
The Stack Trace . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Raising an Exception . . . . . . . . . . . . . . . . . . . . . . . . . 144
Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
15 Just Enough Ruby
147
CONTENTS
Ruby Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
API Documentation . . . . . . . . . . . . . . . . . . . . . . . 151
Ruby Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Using IRB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Whitespace and Line Endings . . . . . . . . . . . . . . . . . . . . . 152
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
The Heart of Programming . . . . . . . . . . . . . . . . . . . . . . 153
Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Object-Oriented Terminology . . . . . . . . . . . . . . . . . . . . . 154
Houses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Vehicles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Limitations of Metaphors . . . . . . . . . . . . . . . . . . . . 156
Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Dot Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Question and Exclamation Methods . . . . . . . . . . . . . . 161
Initialize Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Method Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Instance Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
CONTENTS
xi
179
xii
CONTENTS
Yield Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
ERB Delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Introducing View Helpers . . . . . . . . . . . . . . . . . . . . . . . 186
The Rails Layout Gem . . . . . . . . . . . . . . . . . . . . . . . . 188
Basic Boilerplate . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Adding Boilerplate . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Viewport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Title and Description . . . . . . . . . . . . . . . . . . . . . . 191
Asset Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Assets Without Rails . . . . . . . . . . . . . . . . . . . . . . 193
Assets With Rails . . . . . . . . . . . . . . . . . . . . . . . . 193
Navigation Links . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Introducing Partials . . . . . . . . . . . . . . . . . . . . . . . 195
Introducing Link Helpers . . . . . . . . . . . . . . . . . . . . 196
Navigation Partial . . . . . . . . . . . . . . . . . . . . . . . . 197
Navigation Links Partial . . . . . . . . . . . . . . . . . . . . 198
Flash Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Creating Flash Messages . . . . . . . . . . . . . . . . . . . . 199
Flash and Flash Now . . . . . . . . . . . . . . . . . . . . . . 200
Explaining the Ruby Code . . . . . . . . . . . . . . . . . . . 201
The Flash Messages Partial . . . . . . . . . . . . . . . . . . . 202
HTML5 Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Application Layout . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Simple CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
CONTENTS
xiii
209
CONTENTS
xiv
237
243
CONTENTS
xv
Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Params Hash . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Process_form Method . . . . . . . . . . . . . . . . . . . . . . 250
Test the Application . . . . . . . . . . . . . . . . . . . . . . . . . . 251
The Validation Problem . . . . . . . . . . . . . . . . . . . . . . . . 253
Remove the Contact Page . . . . . . . . . . . . . . . . . . . . . . . 254
Implementing the Rails Way . . . . . . . . . . . . . . . . . . . . 255
ActiveModel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Create a New Contact Page . . . . . . . . . . . . . . . . . . . . . . 259
Seven Controller Actions . . . . . . . . . . . . . . . . . . . . . . . 260
Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Mass-Assignment Vulnerabilities . . . . . . . . . . . . . . . . 263
Private Methods . . . . . . . . . . . . . . . . . . . . . . . . . 264
Routing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Change Navigation Links . . . . . . . . . . . . . . . . . . . . . . . 266
Test the Application . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Git . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
20 Send Mail
269
CONTENTS
xvi
281
293
CONTENTS
xvii
309
Segment.io . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Accounts You Will Need . . . . . . . . . . . . . . . . . . . . . . . 310
Installing the JavaScript Library . . . . . . . . . . . . . . . . . . . 311
Page View Tracking with Turbolinks . . . . . . . . . . . . . . . . . 313
Event Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
CONTENTS
xviii
321
CONTENTS
xix
351
CONTENTS
xx
369
CONTENTS
xxi
381
Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Financial Backers . . . . . . . . . . . . . . . . . . . . . . . . 382
Technical Editors . . . . . . . . . . . . . . . . . . . . . . . . 382
Editors and Proofreaders . . . . . . . . . . . . . . . . . . . . 383
Photos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
xxii
CONTENTS
Chapter 1
Free Offer
I want you to have the most current version of this book. Im making the online
edition available to you for free. Youll also have access to Book One, the first
book in the series, which provides important guidance and background as you
learn Rails.
I work hard to keep the book up to date. If youve obtained the book from
Amazon or another retailer, theres no easy way to inform you of improvements
or corrections, but get the online edition and youll always have an up-to-date
book. Theres also a PDF available for download.
I recommend using the online edition as you build the tutorial application, especially if you want to copy and paste code. Look for the link Free Online
Edition when you visit the site:
learn-rails.com
Use the invitation code:
AMAZON300B2
This book is the foundation for the Capstone Rails Tutorials, which Ive written
for beginners who have finished this book and are ready for more. When youve
1
completed the book, I hope youll consider purchasing the entire Capstone
Rails Tutorials series. I promise there is no better way to learn Rails.
Chapter 2
Introduction
Welcome. This is Book Two in my Ruby on Rails book series.
In this book, youll build a working web application so youll gain hands-on
experience. Along the way, youll practice techniques used by professional
Rails developers. And Ill help youll understand why Rails is a popular choice
for web development.
I suggest you read Book One at your leisure to get the big picture thats missing from other tutorials. Read it anywhere, on your phone or tablet, to gain
background understanding when you are not at your computer. It introduces
key concepts so youll have a solid foundation for continued study. But you
can start this book before you finish Book One.
This book (Book Two) is for hands-on learning so youll need your computer
to follow this tutorial.
Is It for You?
You dont need to be a programmer to succeed with this book. Youll be surprised how quickly you become familiar with the Unix command line interface
and the Ruby programming language even if youve never tried programming
3
CHAPTER 2. INTRODUCTION
before.
Book One provides a primer for the Unix command line, the terminal, and a text
editor. If you have no experience with Unix commands, the terminal window,
or a text editor, read Book One first. Everything else you need is self-contained
in this book.
With this book, my aim is to introduce you to Rails and the techniques of web
application development so you can launch a startup or begin a new career.
What To Expect
There is deep satisfaction in building an application and making it run. With
this book, Ill give you everything you need to build a real-world Rails application. More importantly, Ill explain everything you build, so you understand
how it works. There will be no mystery code.
When youve completed this tutorial, you will be ready for more advanced selfstudy, including the Capstone Rails Tutorials, textbook introductions to Rails
such as Michael Hartls Ruby on Rails Tutorial, or workshops and code camps
that provide intensive training in Ruby on Rails.
WHAT TO EXPECT
developer, youll need to start building applications, and Book One contains a
chapter, Crossing the Chasm, that will give you a strategy for building your
own applications.
CHAPTER 2. INTRODUCTION
tutorial in the Capstone Rails Tutorials series, so theres no mystery code. Each
application can be generated in a few minutes with the Rails Composer tool,
which professional developers use to create starter applications.
The RailsApps Project is solely supported by sales of the books and the Capstone Rails Tutorials. If you purchase the Capstone Rails Tutorials, youll keep
the project going. And youll have my sincere appreciation for your support.
Versions
This book requires a specific version of Rails (the newest at the time it was
revised) and shows how to install the latest version of Rails. The Get Started
chapter provides details.
Rails changes often, so you must work from the newest edition of this book to
avoid problems. If youre not already reading the online edition of this book,
I provide an invitation code so you can get the online edition of this book for
free. For readers who obtained this book from Amazon or another retailer, the
free offer is on the first page of this book, after the table of contents.
Staying In Touch
If you obtained this book from Amazon or another retailer, take a moment to
get on the mailing list for the book. Ill let you know when I release updates to
the book.
Get on the mailing list for the book
WHAT TO EXPECT
the model-view-controller design pattern by creating the components manually. Lastly, though every other Rails tutorial shows how to use a database, this
book doesnt, because I want the book to be a short introduction and I believe
the basic principles of a web application stand out more clearly without adding
a database to the application. Though this tutorial is not a typical Rails introduction, I hope youll agree that it does a good job in preparing Rails beginners
for continued study, whether it is a course or more advanced books.
CHAPTER 2. INTRODUCTION
Chapter 3
Example Code
If you follow this tutorial closely, youll have a working application that closely
matches the example app in the learn-rails GitHub repository. If your application doesnt work after following the tutorial, compare the code to the example
app in the GitHub repository, which is known to work.
Version Check
The code in this tutorial was tested by many people and worked flawlessly at the
time this was written. The learn-rails example application on GitHub serves as
a reference implementation if you have problems. The example application
is updated more frequently than the published tutorial.
Youll find the version number and release date on the first page of this book
9
10
(under the book title). Check the learn-rails GitHub repository to find out if
you have the newest version of the book. The README page on the GitHub
repo always shows the most recent version number for the book and the tutorial
application.
Dont worry if the learn-rails GitHub repository seems complicated or difficult
to understand. Youll get to know GitHub over time and it will become familiar.
For now, it is only important to know that it is there in case you have problems.
Take a moment now to look at the open issues on GitHub to see what problems
you may encounter as you work your way through the tutorial. You can look at
the closed issues to see some of the solved problems.
If you have trouble building the application in this book, and suspect something
may be out of date, you can compare the Gemfile in the repo to see if weve
changed gems or specified version numbers to avoid compatibility issues.
You can also check the CHANGELOG and look at recent commits to see the
current state of the application.
11
If your questions arent answered on Stack Overflow, try the Reddit forum:
/r/learnrubyonrails
The Reddit forum has its own Slack channel for real-time messaging:
learnrubyonrails-slack.herokuapp.com
I sincerely hope you wont encounter obstacles as you build the tutorial application. Thousands of beginners have successfully completed the book and,
unless a gem has recently changed, you should have no problem.
Now lets consider where to look for help when you are working on your own
Rails projects.
12
can also visit the Quora question-and-answer site for topics devoted to Rails
and Ruby.
References
Here are suggestions for the most important references.
If you feel overwhelmed by all the links, remember that you can use this book
to build the tutorial application without any additional resources. Right now,
its important to know additional help is available when you need it.
RailsGuides
The Rails Guides are Railss official documentation, written for intermediatelevel developers who already have experience writing web applications. The
Rails Guides are an excellent reference if you want to check the correct syntax
for Rails code. Youll be able to use the Rails Guides after completing this
tutorial.
Cheatsheets
Tobias Pfeiffer has created a useful Rails Beginner Cheat Sheet that provides a
good overview of Rails syntax and commands.
Even better than a cheatsheet, for Mac users, is an application named Dash
that offers fingertip access to reference documentation for Ruby, Rails, HTML,
CSS, JavaScript, and many other languages and frameworks.
13
API Documentation
The API documentation for Ruby and Rails shows every class and method.
These are extremely technical documents (the only thing more technical is
reading the source code itself). The documents offer very little help for beginners, as each class and method is considered in isolation, but there are times
when checking the API documentation is the only way to know for certain how
something works.
Rails Documentation - official API docs
Rails Searchable API Doc - alternative interface for the API docs
apidock.com/rails - Rails API docs with usage notes
apidock.com/ruby - Ruby API docs with usage notes
Omniref - Ruby and all gem API docs with questions and answers
Omniref is the best place to ask questions about anything in the Ruby or Rails
API.
I recommend Dash as a tool to look up classes, modules, and methods in Ruby
and Rails. Dash is a Mac OS X app; use Zeal on Linux. Dash and Zeal run
offline (they dont need an Internet connection) so you can use them anywhere.
14
Rails developers is friendly and eager to help beginners. If you are near a
Rails meetup, it is really worthwhile to connect to other developers for help
and support. You may find a group that meets weekly for beginners who study
together.
Local user groups often sponsor hack nights or hackathons which can be evening
or weekend collaborative coding sessions. You dont have to be an expert. Beginners are welcome. You can bring your own project which can be as simple
as completing a tutorial. You will likely find a study partner at your level or a
mentor to help you learn.
If you are a woman learning Rails, look for one of the free workshops from
RailsBridge or Rails Girls. These are not exclusively for women; everyone
considered a minority in the tech professions is encouraged to participate;
and men are included when invited by a woman colleague or friend.
Pair Programming
Learning to code is challenging, especially if you do it alone. Make it social
and youll learn faster and have more fun.
Theres a popular trend in the workplace for programmers to work side-by-side
on the same code, sharing a keyboard and screen. Its effective, both to increase
productivity and to share knowledge, and many coders love it. When programmers are not in the same office, they share a screen remotely and communicate
with video chat.
Look for opportunities to pair program. Its the best way to learn to code,
even if your pairing partner is only another beginner. Learn more about pair
programming on the site pairprogramwith.me and find a pairing partner at codermatch.me or letspair.net.
Remote pair programming requires tools for screen sharing and video chat.
Pairing sessions often use:
15
Google+ Hangouts
Screenhero
Floobits
Cloud9 IDE
Nitrous.io
16
Code Review
Code review is an essential part of the development process. Theres always
more than one way to implement a feature, and some ways are better than
others, but you may not know it unless you ask someone to look at your code.
When you pair with a mentor, you get the benefit of code review. But even
if you dont have a mentor, you can get code review online. StackExchange,
the parent of StackOverflow, has a free site for code review, and a new service
promises code review as a service:
codereview.stackexchange.com
devinput.io
Expert code review will accelerate your learning faster than anything else.
Knowing where to go for help is important; it is just as important to stay current.
Staying Up-to-Date
Rails changes frequently and its community is very active. Changes to Rails,
expert blog articles, and new gems can impact your projects, even if you dont
work full-time as a Rails developer. Consequently, I urge you to stay up-to-date
with news from the community.
For daily news about Rails, check Peter Coopers RubyFlow site which lists
new blog posts from Rails developers each day.
I urge you to sign up for two weekly email newsletters:
Ruby Weekly
Green Ruby News
STAYING UP-TO-DATE
17
Another weekly email newsletter is more technical, and focused on code arriving in the next version of Rails:
This Week in Rails
If you like podcasts, check out Ruby Rogues and Envy Labss Ruby5.
Finally, you can follow @rails_apps on Twitter for news about the RailsApps
project.
18
Chapter 4
19
20
GitHub
Rails developers use GitHub for collaboration and remote backup of projects.
For this tutorial, I suggest you get a free personal GitHub account if you dont
already have one. As a developer, your GitHub account establishes your reputation in the open source community. If youre seeking a job as a developer,
employers will look at your GitHub account. When you work with other developers, they may check to see what youve worked on recently. Dont be
reluctant to set up a GitHub account, even if youre a beginner. It shows you
are serious about learning Rails.
Youll be asked to provide a username. This can be a nickname or short version
of your real name (for example, your Twitter username).
Youll be asked to provide an email address. Its very important that you use
the same email address for your GitHub account that you use to configure Git
locally (there will be more about configuring Git later). If you create a Heroku
account to deploy and host your Rails applications, you should use the same
email address.
After you create your GitHub account, log in and look for the button Edit Your
Profile. Take a few minutes to add some public information to your account.
It is really important to provide your real name and a public email address.
Displaying your real name on your GitHub account makes it easy for people
to associate you with your work when they meet you in real life, for example
at a meetup, a hackathon, or a conference. Providing a public email address
makes it possible for other developers to reach you if you ask questions or
submit issues. If you can, provide a website address (even just your Twitter
or Facebook page). In general, you wont be exposed to stalkers or spammers
(except some recruiters) if you are open about yourself on GitHub.
Later Ill show you how to set up and use Git and GitHub.
MAILCHIMP
21
MailChimp
This tutorial shows how website visitors can sign up to receive a newsletter
provided by a MailChimp mailing list. MailChimp allows you to send up to
12,000 emails/month to a list of 2000 or fewer subscribers for free. There is no
cost to set up an account.
After you have set up a MailChimp account, create a new mailing list where
you can collect email addresses of visitors who have asked to subscribe to a
newsletter. The MailChimp Lists page has a button for Create List. The
list name and other details are up to you.
If you get frustrated with the complex and confusing MailChimp interface, try
to remember that the friendly MailChimp monkey is laughing with you, not at
you.
SendGrid
Earlier editions of this book showed how to use a Gmail account to send email
from the application. Google has taken steps to make Gmail more secure and
now it can be difficult to send email from a Rails application using Gmail.
This tutorial provides instructions for SendGrid. SendGrid offers a free account
that allows you to send 12,000 messages per month for free.
Scroll to the bottom of the SendGrid pricing page to see details about the free
plan. Click the Try for Free link to set up an account. No credit card is
needed.
Heroku
Well use Heroku to host the tutorial application so anyone can reach it.
22
To deploy an app to Heroku, you must have a Heroku account. Visit https://signup.heroku.co
to set up an account.
Be sure to use the same email address you used to register for GitHub. Its
very important that you use the same email address for GitHub and Heroku
accounts.
Chapter 5
Get Started
Before you can start building, youll need to install Ruby (the language) and
Rails (the gem). Ill provide links to installation instructions that are up to
date. Even if youve already installed Rails, please review the instructions to
make sure your development environment is set up correctly. Other books and
tutorials often skip important details.
24
YOUR COMPUTER
25
With the online edition, youll be able to copy and paste the code without any
problem. The ePub version (using Apple iBooks) also preserves line breaks and
indentation when copying code.
Copying without line breaks will cause code errors. Youll lose line breaks when
copying code with the following versions:
PDF version on Mac OS X using the Preview application
mobi (Kindle)
If you use Adobe Acrobat youll be able to copy the line breaks (though indenting
is lost). You can also open a PDF file in Chrome or Safari web browsers and copy
code with line breaks. With the mobi (Kindle) version, youll have to carefully
reformat the code after pasting into your text editor.
Indentation makes code more readable, so try to preserve the indentation you see
in the code samples. In YAML files (with the file extension .yml), indentation is
required (your application will break without it).
Your Computer
You can develop web applications with Rails on computers running Mac OS
X, Linux, or Microsoft Windows operating systems. Most Rails developers
use Mac OS X or Linux because the underlying Unix operating system has
long been the basis for open source programming.
Later in this chapter, Ill give links to installation instructions for Mac OS X
and Linux.
For Windows users, I have to say, installing Rails on Windows is frustrating
and painful. Readers and workshop students often tell me that theyve given
26
Hosted Computing
If you are using Windows, or have difficulty installing Ruby on your computer,
try using Cloud9.
Cloud9 provides a hosted development environment. That means you set up
an account and then access a remote computer from your web browser. The
Cloud9 service is free for ordinary use. There is no credit card required to set
up an account. Youll only be charged if you add extra computer memory or
disk space (which you dont need for ordinary Rails development).
The Cloud9 service gives you everything you need for Rails development, including a Unix shell with Ruby pre-installed, plus a browser-based file manager and text editor. Any device that runs a web browser will give you access
to Cloud9, including a tablet or smartphone, though you need a broadband connection, a sizable screen, and a keyboard to be productive.
27
On the Mac, search for the Mac OS X Terminal application by pressing the
Command-Spacebar combination (which Apple calls Spotlight Search) and
searching for Terminal. The magnifying glass in the upper right corner of
your screen will also launch Spotlight Search. Or look in the Applications/Utilities/ folder for the Terminal application. Youll need to click the
name of the application to launch the Terminal.
For Linux or Windows, The Command Line Crash Course explains how to
launch a terminal application.
Launch your terminal application now.
Try out the terminal application by entering a shell command.
$ whoami
Dont type the $ character. The $ character is a cue that you should enter a
shell command. This is a longtime convention that indicates you should enter
a command in the terminal application or console.
The Unix shell command whoami returns your username.
Dont type the $ prompt.
You might see:
command not found: $
28
Installing Ruby
Your first challenge in learning Rails is installing Ruby on your computer.
Frankly, this can be the most difficult step in learning Rails because no tutorial
can sort out the specific configuration of your computer. Get over this hump
and everything else becomes easy.
The focus of this book is learning Rails, not installing Ruby, so to keep the
book short and readable, Im going to give you links to articles that will help
you install Ruby.
Youll spend at least an hour installing Ruby and Rails, so be sure to allow
enough time for the task.
Mac OS X
See this article for Mac OS X installation instructions:
Install Ruby on Rails - Mac OS X
Ubuntu Linux
See this article for Ubuntu installation instructions:
Install Ruby on Rails - Ubuntu
Hosted Computing
Cloud9 is a browser-based development environment. Cloud9 is free for small
projects. If you have a fast broadband connection to the Internet, this is your
best choice for developing Rails on Windows. And it is a good option if you
have any trouble installing Ruby on Mac or Linux because the Cloud9 hosted
environment provides everything you need, including a Unix shell with Ruby
YOUR WORKSPACE
29
and RVM pre-installed, plus a browser-based file manager and text editor. Using a hosted development environment is unconventional but leading developers do so and it may be the wave of the future.
See this article for Cloud9 installation instructions:
Install Ruby on Rails - Cloud9
The article shows how to get started with Cloud9.
If you use Cloud9, be sure to pick Custom for your workspace, not the
Ruby or Ruby on Rails workspaces that provide a prebuilt Rails application. Were building from scratch in this tutorial.
Windows
Here are your choices for Windows:
Use the Cloud9 hosted development environment
Install the Railsbridge Virtual Machine
Use RubyInstaller for Windows
Cloud9 is ideal if you have a fast Internet connection. If not, download the
Railsbridge Virtual Machine to create a virtual Linux computer with Ruby 2.2
and Rails 4.2 using Vagrant. Other tutorials may suggest using RailsInstaller,
but it will not provide an up-to-date version of Ruby or Rails. Also, RVM does
not run on Windows.
Your Workspace
Take a moment to think about where on your computer youll do your work
and store your files. You may have a documents/ folder. You could make a
30
In this tutorial, the terms folders and directories mean the same thing.
Use the Unix cd command to change directories.
When you enter the Unix command cd ~, youll move to your home (or
user) directory. The squiggly ~ tilde character is a Unix shortcut that indicates your home folder.
The Unix pwd command shows the present working directory, where you are.
The Unix mkdir command creates an empty folder and we move into it with
the Unix cd command.
31
A major release includes new features, including changes which break backward compatibility. For example, switching from Rails 3.2 to Rails 4.0 required
a significant rewrite of every Rails application.
A minor release introduces new features but doesnt break anything. For example, Rails 3.2 added the asset pipeline, and Rails 4.2 added the Active Job
feature for background processing.
A patch release fixes bugs but doesnt introduce significant features. Usually
this means you can change the version number in the Gemfile and run bundle
update without making any other changes to your application.
Youve got Ruby version 2.3.0, patch level p0 (Ruby versions add an extra
patch level to semantic versioning). If youve got a newer version of Ruby, no
problem; minor updates to Ruby dont affect Rails.
32
Try:
$ rails -v
If you have Rails 4.1 or older versions, you must update to Rails 4.2. See the
Installing Rails instructions for your computer.
Versions such as 5.0.0.beta1 or 5.0.0.rc1 are beta versions or release
candidates. You can use a release candidate in the weeks before a final release
becomes available.
If youve got Rails 4.2.5 or newer, thats fine. It means minor bugs have been
fixed since this was written, but the book is still current. You can check for the
current version of Rails here.
RVM
I promised that this book would introduce you to the practices of professional
Rails developers. One of the most important utilities youll need in setting up
RVM
33
You can see a list of available gemsets associated with the current Ruby version:
$ rvm gemset list
34
After youve worked on a few Rails applications, youll see several projectspecific gemsets if you are using RVM in the way most developers do.
RVM is not the only utility you can use to manage multiple Ruby versions.
Some developers like Chruby, rbenv, or others. Dont be worried if you hear
debates about RVM versus Chruby or rbenv; developers love to compare the
merits of their tools. RVM is popular, well-supported, and an excellent utility
to help a developer install Ruby and manage gemsets; thats why we use it.
Project-Specific Gemset
For our learn-rails application, well create a project-specific gemset using
RVM. Well give the gemset the same name as our application.
By creating a gemset for our tutorial application, well isolate the current version of Rails and the gems we need for this project. Whether you use RVM
or another Ruby version manager, this will introduce you to the idea of sandboxing (isolating) your development environment so you can avoid conflicts
among projects.
After we create the project-specific gemset, well install the Rails gem into the
gemset. Enter these commands:
$ rvm use ruby-2.3.0@learn-rails --create
$ gem install rails
PROJECT-SPECIFIC GEMSET
35
You should see the message Rails 4.2.5 (or something similar).
Now lets explore the rails new command and get started building the tutorial application.
36
Chapter 6
Starter Applications
Rails provides a framework; that is, a software library that provides utilities,
conventions, and organizing principles to allow us to build complex web applications. Without a framework, wed have to code everything from scratch.
Rails gives us the basics we need for many websites.
Still, the framework doesnt give us all the features we need for many common
types of websites. For example, we might want users to register for an account
and log in to access the website (user management and authentication). We
might want to restrict portions of our website to just administrators (authorization). We also might want to add gems that enhance Rails to aid development
(gems for testing, for example) or improve the look and feel of our application
(the Bootstrap or Foundation front-end frameworks). Developers often mix and
match components to make a customized Rails stack.
Developers often use a starter application instead of assembling an application
37
38
from scratch. You might call this a template but we use that term to refer to
the view files that combine HTML with Ruby code to generate web pages. Most
experienced developers have one or more starter applications that save time
when beginning a new project. The RailsApps project was launched to provide
open source starter applications so developers could collaborate on their starter
applications and avoid duplicated effort. After you gain some skill with this
tutorial, you might use the RailsApps starter apps to instantly generate a Rails
application with features like authentication, authorization, and an attractive
design. At the end of this book, youll learn about Rails Composer, a tool for
building starter applications.
For now, well begin with the Rails default starter application.
We already created a project-specific gemset using RVM. Make sure its ready
to use:
$ rvm use ruby-2.3.0@learn-rails
$ rvm gemset list
gemsets for ruby-2.3.0...
(default)
global
=> learn-rails
You should see an arrow pointing to the learn-rails gemset. If not, go back
to the previous Get Started chapter.
39
This is your project directory. It is also called the application root directory.
Youll spend all your time inside this folder.
Type the ls command to show the folders and files in a directory. Soon well
learn more about each of these folders and files.
40
$ ls
Gemfile
Rakefile
Gemfile.lock app
README.md
bin
config
config.ru
db
lib
log
public
test
tmp
vendor
If you see ERROR: Gemset learn-rails does not exist, perhaps you overlooked an earlier step in the Project-Specific Gemset section (in the previous
chapter) where we created the learn-rails gemset. No matter, you can create it
now:
$ rvm use ruby-2.3.0@learn-rails --create --ruby-version
$ gem install rails
The -ruby-version argument creates two files, .ruby-version and .rubygemset, that set RVM every time we cd to the project directory. Without these
two hidden files, youd need to remember to enter rvm use ruby-2.3.0@learn-rails
every time you start work on your project after closing the console.
You can confirm youve created the two hidden files:
$ ls -1pa
./
../
41
.gitignore
.ruby-gemset
.ruby-version
Gemfile
Gemfile.lock
README.rdoc
Rakefile
app/
bin/
config/
config.ru
db/
lib/
log/
public/
test/
tmp/
vendor/
The a flag in the Unix ls -1pa command displays hidden files. Each hidden
file is listed with a dot (period or full stop) at the beginning of the filename.
Youll notice .ruby-gemset and .ruby-version.
Youll also see two special files which are not files at all:
./ - an alias that represents the current directory
../ - an alias that represents the parent directory
42
43
Thats a brief diversion into Unix; lets try running our new Rails application.
Alternatively, to save typing, you can abbreviate the rails server command:
$ rails s
Youll see:
=> Booting WEBrick
=> Rails 4.x.x application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0)....
=> Ctrl-C to shutdown server
[...] INFO WEBrick 1.3.1
[...] INFO ruby 2.x.x (2014-02-24) [x86_64-darwin13.0]
[...] INFO WEBrick::HTTPServer#start: pid=38534 port=3000
The rails server command launches the default WEBrick web server that
is provided with Ruby.
44
You need to install Node.js. For help, see Install Ruby on Rails - Ubuntu.
45
46
47
48
Chapter 7
49
50
Project Directory
Use the Unix ls command to list the contents of the project directory. For a
one-column list that shows each subdirectory (marked with a slash), well add
the -1p option to the command.
$ ls -1p
Youll see:
Gemfile
Gemfile.lock
README.rdoc
Rakefile
app/
bin/
config/
config.ru
db/
lib/
log/
public/
tmp/
vendor/
Now is a good time to open a file browser window and look at the contents of
the project directory. On the Mac, theres a command you can use to open the
graphical file browser from the console. If youre in the project directory, type
open .. The period (or dot) is a Unix symbol that means the directory Im
in.
$ open .
Youll learn more about each file and folder as you proceed through the tutorial.
51
52
config/
db/
public/
53
Whether you user the file browser, Unix commands, or your text editor, you are
looking at the same file system.
Most of the work of developing a Rails application happens in the app/ folder.
Earlier we described Rails as a set of files organized with a specific structure.
We said the structure is the same for every Rails application. The app/ directory
54
is a good example. The six folders in the app/ directory are the same in every
Rails application. This makes it easy to collaborate with other Rails developers,
providing consistency and predictability.
assets
controllers
helpers
mailers
models
views
You may recall our earlier description of Rails from the perspective of a software architect. In this folder, youll see evidence of the modelviewcontroller
design pattern. Three folders named models/, views/, and controllers/ enforce
the software architects separation of concerns and impart structure to our
code. As you build the application, well explain the role of the MVC components in greater detail.
Two folders, mailers/ and helpers/, play supporting roles. The mailers folder
is for code that sends email messages. The helpers folder is for Rails view
helpers, snippets of reusable code that generate HTML. Later, when we learn
more about views, well say view helpers are like macros that expand a short
command into a longer string of HTML tags and content.
55
If you join a project to work on a large and complex Rails application, you
may see folders such as these in the app/ directory. As an application grows in
complexity, an experienced software architect may suggest reducing the size of
models and controllers by moving code to POROs (plain old Ruby objects).
Code in any folder in the app/ directory is shared throughout a Rails application without any additional configuration (in contrast, code you add to the
lib/ directory is only available with some extra work). Rails provides a basic
modelviewcontroller framework but it is often necessary to extend it.
Use the cd .. command (change directory dot dot) to return to the project
directory.
$ cd ..
$ pwd
/Users/danielkehoe/workspace/learn-rails
As a Rails developer, youll spend most of your time navigating the hierarchy
of folders as you create and edit files. And because Rails provides a consistent
structure, youll quickly find your way on any unfamiliar project.
56
Chapter 8
58
we may be saving typos or unfinished code that doesnt work. Every so often,
we get to a point where a task is finished; weve fixed all our errors and our
code runs. We want to preserve the state of our work. Thats when we need a
version control system.
A version control system does more than a software applications Save command. Like a Save command, it preserves the current state of our files. It
also allows us to add a short note that describes the work weve done. More
importantly, it archives a snapshot of the current state in a repository where it
can be retrieved if needed.
Heres where the time travel comes in. We can go back and recover the state
of our work at any point where we committed a snapshot to the repository. In
software development, travel to the past is essential because we often make
mistakes or false starts and have to return to a point where we know things
were working correctly.
What about time travel to the future? Often we need to try out code we may
decide to discard, without disturbing work weve done earlier. Version control
systems allow us to explore alternative futures by creating a branch for our
work. If we like what weve done in our branch, we can merge it into the main
trunk of our software project.
Unlike time travel in the movies, we cant travel back to any arbitrary point in
the flow of time. We can only travel to past or future states weve marked as
significant by checking our work into the repository.
Git
The dominant version control system among Rails developers is Git, created
by the developer of the Linux operating system.
Unlike earlier version control systems, Git is ideal for wide-scale distributed
open source software development. Combined with GitHub, the social coding website, Git makes it easy to share and merge code. When you work with
GIT
59
others on a project, your Git commit messages (the notes that accompany your
snapshot) offer a narrative about the progress of the project. Well-written commit messages describe your work to co-workers or open source collaborators.
GitHubs support for forking (making your own copy of a repository) makes
it possible to take someone elses project and modify it without impacting the
original. That means you can customize an open source project for your own
needs. You can also fix bugs or add a feature to an open source project and
submit a pull request for the project maintainer to add your work to the original.
Fixing bugs (large or small) and adding features to open source projects are how
you build your reputation in the Rails community. Your GitHub account, which
shows all your commits, both to public projects and your own projects, is more
important than your resum when a potential employer considers hiring you
because it shows the real work you have done.
Collaboration is easy when you use a branch in Git. If you and a coworker
are working on the same codebase, you can each make a branch before adding
to the code or making changes. Git supports several kinds of merges, so you
can integrate your branch with the trunk when your task is complete. If your
changes collide with your coworkers changes, Git identifies the conflict so you
can resolve the collision before completing the merge.
All the power of Git comes at a price. Git is difficult for a beginner to learn,
largely because many of its procedures have no real-world analog. Have you
noticed how time travel movies require mental gymnastics, especially when
you try to make sense of alternative futures and intersecting timelines? Git is a
lot like that, mostly because we use it to do things we dont ordinarily do in the
real world.
In this tutorial, you wont encounter Gits advanced procedures, like resolving
merges or reverting to earlier versions. Well stick to the basics of archiving
our work (and in one case, discarding work that weve done for practice). You
can build the tutorial project without using Git. But I urge you to use Git and
a GitHub account for this project, for two reasons. First, with your tutorial
application on GitHub, youll show potential employers or collaborators that
youve successfully built a useful, functioning Rails application. More impor-
60
tantly, you must get to know Git if you plan to do any serious coding, either as
a professional or a hobbyist.
Before I show you Git commands, I want to mention that some people use
graphical client applications to manage Git. Mac OS X has GitHub for Mac, Git
Tower, and other Mac Git clients. Graphical applications for Git are useful for
colleagues who dont use a Terminal application, such as graphic designers or
writers. Theres no need for you to install these applications. Every developer
Ive met uses Git from the command line. It will take effort to master Git; the
commands are not intuitive. But it is absolutely necessary to become familiar
with Git basics.
Before you do any work on the tutorial application, Ill show you the basics of
setting up and using Git.
Is Git Installed?
As a first step, make sure Git is installed on your computer:
$ which git
/usr/bin/git
$ git version
git version ...
If Git is not found, install Git. See the article Rails with Git and GitHub for
installation instructions.
Is Git Configured?
Make sure Git knows who you are. Every time you update your Git repository
with the git commit command, Git will identify you as the author of the
changes.
CREATE A REPOSITORY
61
You should see your name and email address. If not, configure Git:
$ git config --global user.name "Real Name"
$ git config --global user.email "[email protected]"
Use your real name so people will associate you with your work when they
meet you in real life. Theres no reason to use a clever name unless you have
something to hide.
Use the same email address for Git, your GitHub account, and Heroku to avoid
headaches.
Create a Repository
Now well add a Git repository to our project. Its a basic step youll repeat
every time you create a new Rails project.
Extending the time traveler analogy, initializing a Git repository is equivalent
to setting up the time machine.
The git init command sets up a Git repository (a repo) in the project
directory. We add the Unix symbol that indicates Git should be initialized in
the current directory (git init dot):
$ git init .
Initialized empty Git repository in ...
It creates a hidden folder named .git/ in the project directory. You can peek at
the contents:
62
$ ls -1p .git
HEAD
branches/
config
description
hooks/
info/
objects/
refs/
All Git commands operate on the hidden files. The hidden files record the
changing state of your project files each time you run the git commit command. There is no reason to ever edit files inside the hidden .git/ folder (doing
so could break your time machine).
GitIgnore
The hidden .git/ folder contains the Git repository with all the snapshots of
your changing project. The snapshots are highly compressed, only containing
records of changes, so the repository takes up very little file space relative to
the project as a whole.
Not every file should be included in a Git snapshot. Here are some types of
files that should be ignored:
log files created by the web server
database files
configuration files that include passwords or API keys
Git gives us an easy way to ignore files. A hidden file in the project directory
named .gitignore can specify a list of files that are never seen by Git. The
rails new command creates a .gitignore file with defaults that include log
GIT WORKFLOW
63
files and database files. Later, when we add configuration files that include
secrets, well update the .gitignore file.
Take a look at the contents of the .gitignore file. We use the Unix cat command to display the contents of the file:
$
#
#
#
#
#
cat .gitignore
See http://help.github.com/ignore-files/ for more about ignoring files.
If you find yourself ignoring temporary files generated by your text editor
or operating system, you probably want to add a global ignore instead:
git config --global core.excludesfile '~/.gitignore_global'
For a .gitignore file that ignores more, see an example .gitignore file from the
RailsApps project.
Git Workflow
Your workflow with Git will move through four distinct phases as you add or
edit files.
Untracked Files
The first phase is a dirty state of untracked and changed files, before any
snapshot. The git status command lists all folders or files that are not
checked into the repository.
64
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#
(use "git add ..." to include in what will be committed)
#
# .gitignore
# Gemfile
# Gemfile.lock
# README.rdoc
# Rakefile
# app/
# bin/
# config.ru
# config/
# db/
# lib/
# log/
# public/
# vendor/
nothing added to commit but untracked files present (use "git add" to track)
Here the git status command tells us that we have many untracked files.
We have created new files and they are saved on the computers hard disk but
nothing has been recorded in the Git repository.
Staging
I call this step, Pose for your snapshot.
Recording files in the Git repository takes two steps: staging and committing.
There will be times when you change many files at once. For example, you
may fix a bug, add a new graphic, and change a form. You might think youd
like to have Git automatically record all the changes as you save each file. But
the story of your project would be confusing and overly detailed. Git requires
you to mark one or more files (staging) before recording the changes (committing). This gives you fine-grained control over the recorded history of your
project.
GIT WORKFLOW
65
Adding individual files allows you to selectively record the history of your
project. For example, you might stage and commit a series of bug fixes before
you stage and commit new features. Applying the time traveler analogy, it will
be easier to travel back to look at bug fixes if they are not mixed in with new
features.
More often, youll mark all the files to be staged. Do so now:
$ git add -A
Running git status will show you a long list of files that are staged and
ready to commit.
There are three forms of the git add command:
git add foo.txt adds a file named foo.txt
git add . adds all new files and changed files, except deleted files
git add -A adds everything, including deletions
If it seems nonsensical that the command git add -A adds deletions, dont
worry. Like time travel, Git will stretch your understanding of what makes
sense.
Most often, you can simply use the git add -A form of the command.
Now that youve marked the files that will be committed to the repository,
youve told everyone to pose, and youre ready to take the snapshot.
66
Committing
The posing step, or staging, gives you an opportunity to select particular files
before you commit.
If youve only worked on one feature, youll likely stage and commit all your
files.
When you make a commit, you include a message that describes the work
youve done. For a time traveler, the commit message is important; you
are leaving a trail to help you find your way into the past. Google will show
you dozens of blog posts about writing better commit messages but common
sense can be your guide. Writing fix registration form to catch blank email
addresses will be more helpful than merely writing fix bugs. And if you
wonder why commit messages are commonly written in the imperative not past
tense (fix not fixed), its a time traveler convention.
Now commit your project to the repository:
$ git commit -m "Initial commit"
Git Log
You can use the git log command to see your project history:
$ git log
commit 8da41eec9e864ed91b4a445d8cefdf7893e2faf6
Author: Daniel Kehoe <[email protected]>
Date:
Fri Dec 18 10:30:12 2015 +0700
GIT WORKFLOW
67
Initial commit
The long string of characters that follows commit is an ID, or marker, that
will help you travel back in time if you need to do so.
If you get stuck in git log, type q to return to the command prompt.
I like to use the git log command with an option for a compact listing:
$ git log --oneline
8da41ee Initial commit
Pushing to GitHub
Weve seen three phases of the Git workflow: untracked, staged, and committed.
A fourth stage is important when you work with others: pushing to GitHub. Its
also important when you access your project from more than one computer or
you want an offsite backup of your work.
The repositories hosted on your GitHub account establish your reputation as a
Rails developer for employers and developers you may work with. Even if your
first project is copied from a tutorial, it shows you are serious about learning
Rails and studying conscientiously.
Did you create a GitHub account? Now would be a good time to add your repo
to GitHub.
Go to GitHub and create a new empty repository for your project. Name the
repository learn-rails and give it a description. If the repository is public,
hosting on GitHub is free. Dont be reluctant to go public with an unfinished
68
The -u option sets up Git so you can use git push in the future without
explicitly specifying GitHub as the destination.
Now you can view your project repository on GitHub at:
https://github.com/YOUR_GITHUB_ACCOUNT/learn-rails
Obviously you must change YOUR_GITHUB_ACCOUNT in the web address to
see your own repository.
Take a look. Its an exact copy of the project on your local computer.
If you havent used GitHub before, take some time to explore. GitHub is absolutely essential to all open source Rails development.
You may notice that the README.rdoc file is automatically incorporated into
the home page of the project repository on GitHub. For our next step, well
update the README file, commit it to the local repo, and push it up to GitHub.
It will be good practice for using Git.
The README
Changing the README file is a good way to practice with Git. Its also a good
habit to edit the README file whenever you create a new project. Its easy to
THE README
69
neglect the README for little projects that youve just started. But replacing
a default README file shows you are a disciplined, conscientious developer
who will be a good collaborator.
The new README file can be brief. Just state your intentions and acknowledge any code youve borrowed. For this project you could say, Excited to
learn Rails with help from the RailsApps project!
In your text editor, open the file README.rdoc and replace the contents:
Learning Rails
==
Learning Rails with a tutorial from the RailsApps project.
GitHub lets you add formatting using your choice of markup syntax, depending
on the file extension you add to the filename:
README.rdoc uses the rdoc syntax
README.md uses the GitHub Flavored Markdown syntax
README.textile uses the Textile syntax
Well use Markdown syntax by adding the == characters after the first line of
text to force a headline.
Theres no requirement that you use Markdown syntax in your README file.
Markdown is a popular way to add formatting to improve readability. For us,
changing the file to Markdown creates a practical exercise in using Git.
Well use the git mv command to rename the file to README.md and save
it.
$ git mv README.rdoc README.md
70
git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
renamed:
You could also use the Unix mv command to rename the file. If you do so,
git status will show the README.rdoc file has been deleted and a new,
untracked README.md file has been created.
Heres our typical workflow. Well stage, commit, and push the change to
GitHub:
$ git add -A
$ git commit -m "update README"
$ git push origin master
Take a look at your GitHub repository (refresh the web page). Very cool! The
README file has been updated.
The git log command will display your project history:
$ git log --oneline
69b9b6c update README
8da41ee Initial commit
You can read more about Git and Rails if you need more information about
working with Git and GitHub for code source control.
Now that youre comfortable with Git, we can begin customizing our new Rails
application.
Chapter 9
Gems
The art of selecting gems is at the heart of Rails development. I explained
earlier that gems are packages of code, software libraries, that have been
developed and tested by other developers. Some gems add functionality or
features to a website. Other gems play a supporting role, making development
easier or implementing basic infrastructure. Gems are open source. They are
available at no charge and can be freely copied and modified.
It is a mark of honor to release a gem for public use, and a developers reputation can be established when a gem becomes popular and widely used. Gems
are often created when a developer has used the same code as a component in
more than one web application. He or she will take time to release the code as
a gem. Thats how the Rails ecosystem was built, gem by gem since 2004.
There is no evaluation or review process in publishing gems. Gems are hosted
on a public server, rubygems.org. Gems are mostly text files (like any other
Ruby code), organized in a particular format with some descriptive information
(in a gemspec file), and compressed and archived as a single file. A single
command, gem push, uploads a gem to the rubygems.org server for anyone to
use.
Over 50,000 gems have been released since rubygems.org was established.
Some of these gems are used by one or two developers on their own projects.
71
CHAPTER 9. GEMS
72
Many others have been neglected and abandoned due to lack of interest. Only
a few thousand gems are popular and widely used. As a Rails developer, you
must master the art of finding and evaluating gems so you can base your applications on the tried-and-true work of others.
There is no single authoritative source of recommendations for gems. The
Ruby Toolbox website categorizes and ranks many gems by popularity, and it
is a good place to begin hunting for useful gems. Other than that, it is useful
to study example applications and search for blog posts to find which gems
are most often recommended. When you find an interesting gem, search Stack
Overflow or Google to see what people are saying. Look at the gems GitHub
repository and check:
How many issues are open? How many are closed?
How recent are the commits of patches or updates?
Is there a CHANGELOG file?
Is the gem well-documented?
How many stars (people watching) or forks (people hacking)?
Popular gems are likely to have many reported issues, some of which are trivial
problems or feature requests. Gems that are actively maintained will have many
closed issues and, ideally, only a few open issues. When you find a gem that
has many open issues and no recently closed issues, youve probably found a
gem that has been abandoned. Also look at the commit log, which youll find
on the GitHub project page in a tab at the top of the page. Regular and recent
activity in the commit log indicates the gem is actively maintained.
Rails Gems
Rails itself is a gem that, in turn, requires a collection of other gems. This
becomes clear if you look at the summary page for Rails on the rubygems.org
73
site. On that page, youll see photos of the Rails core team. More importantly,
youll see a list of gems that are required to use Rails:
actionmailer - framework for email delivery and testing
actionpack - framework for routing and responding to web requests
activerecord - framework for connections to databases
activesupport - utility classes and Ruby library extensions
bundler - utility to manage gems
railties - console commands and generators
sprockets-rails - support for the Rails asset pipeline
These are the runtime dependencies for Rails. Each of these gems has its
own dependencies as well. When you install Rails, a total of 44 gems are
automatically installed in your development environment.
CHAPTER 9. GEMS
74
turbolinks - faster loading of webpages
jbuilder - utility for encoding JSON data
You may not need a SQLite database, SCSS for stylesheets, jQuery or the others, but many developers use these tools so they are included in the default
starter application.
75
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.4.8
- RUBY VERSION: 2.3.0 (2015-12-16 patchlevel 230) [x86_64-darwin14]
- INSTALLATION DIRECTORY: /Users/danielkehoe/.rvm/gems/ruby-2.3.0@learn-rails
- RUBY EXECUTABLE: /Users/danielkehoe/.rvm/rubies/ruby-2.3.0/bin/ruby
- EXECUTABLE DIRECTORY: /Users/danielkehoe/.rvm/gems/ruby-2.3.0@learn-rails/bin
- SPEC CACHE DIRECTORY: /Users/danielkehoe/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-14
- GEM PATHS:
- /Users/danielkehoe/.rvm/gems/ruby-2.3.0@learn-rails
- /Users/danielkehoe/.rvm/gems/ruby-2.3.0@global
.
.
.
If you use RVM, gems are saved to a hidden .rvm folder in your user directory. A global subfolder contains the Bundler gem. If youve followed the
instructions in the Get Started chapter to install Rails, the project-specific
learn-rails subfolder contains the Rails gem. If you use Chruby or Rbenv instead of RVM, your gems will be stored in a different location.
Run the gem which command and youll see where the gems live:
These are details youll never need to know, because Ruby on Rails handles it
for you.
Youll never move or delete gems directly. Instead youll manage gems using
the Bundler utility. The key to Bundler is the Gemfile.
76
CHAPTER 9. GEMS
Gemfile
Every Rails application has a Gemfile. Earlier, I described Rails from the viewpoint of the gem hunter, the developer who wants to assemble an application
from the best open source components he or she can find. To the gem hunter,
the Gemfile is the most important file in the application. It lists each gem that
the developer wants to use.
The Gemfile provides the information needed by the Bundler utility to manage
gems.
Bundlers bundle install command reads the Gemfile, then downloads and
saves each listed gem to the hidden gem folder. Bundler checks to see if the
gem is already installed and only downloads gems that are needed. Bundler
checks for the newest gem version and records the version number in the Gemfile.lock file. Bundler also downloads any gem dependencies and records the
dependencies in the Gemfile.lock file. Between the Gemfile, with its list of
gems that will be used by the application, and the Gemfile.lock file, with its
list of dependencies and version numbers, you have a complete specification of
every gem required to run the application. More importantly, when other developers install your application, Bundler will automatically install all the gems
(including dependencies and correct versions) needed to run the application.
When you deploy the application to production for others to use, automated
deployment scripts (such as those used by Heroku) install all the required gems.
Bundler provides a bundle update command when we want to replace any
gems with newer versions. If you run bundle update, any new gem versions will be downloaded and installed and the Gemfile.lock file will be updated. Be aware that updating gems can break your application, so only update
gems when you have time to test and resolve any issues. You can run bundle
outdated to see which gems are available in newer versions.
If you want to prevent your fellow developers (or yourself) from accidentally
updating gems, you can specify a gem version number for any gem in the Gemfile. The Gemfile gives fine-grained control over rules for updating:
77
78
CHAPTER 9. GEMS
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster...
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running...
gem 'spring'
end
The file you see will be very similar. Some version numbers may be different
if a newer Rails version was released since this was written.
The first line, source https://rubygems.org, directs Bundler to use
the rubygems.org server as a source for any gems.
Notice that the second uncommented line directs Bundler to use Rails and specifies the version number. Its important to specify which version of Rails we
are using. Rails changes frequently and newer versions may not work as we
expect.
Its also wise to specify the Ruby version were using. This is needed for automated deployment scripts such as those used by Heroku. We can add that to
the Gemfile:
79
ruby '2.3.0'
In the Gemfile youll see the gems for a Rails default application, such as
sqlite3, which we described earlier. Other gems are commented out (the lines
begin with the # character). These are suggestions and we can ignore them or
remove them.
We wont use a database for our application but well keep the gem sqlite3
entry. Configuring Rails for no database is complicated; it is easier to keep the
sqlite3 gem and not use it.
The gem sdoc line is useful only when using rake doc:rails command
to generate API documentation so we can remove it.
If you are developing your application on a computer using the Linux operating
system, you may need to uncomment and use the statement gem therubyracer,
platforms: :ruby. Linux doesnt have a built-in JavaScript interpreter so
you must install Node.js in your environment or else add the therubyracer gem
to each project Gemfile. For help, see Install Ruby on Rails - Ubuntu.
If you remove the extra clutter in the Gemfile it will look like this:
source 'https://rubygems.org'
ruby '2.3.0'
gem 'rails', '4.2.5'
# Rails defaults
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
Try it now. Replace the Gemfile with the simplified code above.
CHAPTER 9. GEMS
80
Adding Gems
Ive identified several gems that will be useful for our tutorial application.
I learned of these gems from a variety of different sources:
Ruby Toolbox
RailsCasts
RubyFlow
various blog posts
example code and starter apps on GitHub
recommendations from colleagues
Were adding these gems at the beginning of our development process since
we already know which gems well need. On a real project, youll often discover useful gems and add them to the Gemfile during the ongoing process of
development.
Here are gems well add to the Gemfile:
foundation-rails - front-end framework
gibbon - access to the MailChimp API
high_voltage - for static pages like about
simple_form - forms made easy
Well also add utilities that make development easier:
better_errors - helps when things go wrong
ADDING GEMS
81
Notice that weve specified a version for the foundation-rails gem. We are using
pessimistic versioning for the gem, by using the squiggle operator, so that
we will use version 5.5 or newer, but not version 6.0 or beyond. We want to
stick with the Foundation 5 framework, not the newer Foundation 6 framework,
to avoid some potential errors.
Notice that weve placed three gems inside a group. Specifying a group for
development or testing ensures a gem is not loaded in production, reducing the
applications memory footprint. Rails let you specify groups for development,
test, or production.
82
CHAPTER 9. GEMS
Each time you edit the Gemfile, you will run bundle install and restart
your web server.
If youve got Rails 4.2.5, theres no need to make additional changes to the
Gemfile.
If youve got a newer version of Rails, update the Gemfile. Change this line as
needed:
gem 'rails', '4.2.5'
If you have Rails 5.0 (which was not available when this was written), you
must get a new version of this book (or install Rails 4.2). The newest available
version of the book is listed on the README page of the learn-rails GitHub
repository.
83
$ bundle install
The bundle install command will download the gems from the rubygems.org
server and save them to a hidden directory that is managed by the RVM gemset
youve specified.
Well see all the gems and their dependencies:
Fetching gem metadata from https://rubygems.org/........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.0.4)
Using i18n (0.6.4)
Installing minitest (4.7.4)
.
.
.
(many more gems not shown... you get the idea)
.
.
.
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
You can use your text editor to view the contents of Gemfile.lock and you will
see a detailed listing of every gem and each dependency, with version numbers.
Theres no reason to edit a Gemfile.lock file; if it is ever in error, delete it and
run bundle install to recreate it.
Run gem list to see all the gems that are loaded into the development environment:
$ gem list
The list of gems loaded in the environment is the same as the list specified in the
Gemfile.lock file. Heres how it works. RVM makes a place for the gems to be
stored (the RVM gemset); the Gemfile lists the gems you want to use; bundle
84
CHAPTER 9. GEMS
install reads the Gemfile and installs the gems into the RVM gemset; the
Gemfile.lock file records dependencies and version numbers; and gem list
shows you the gems that are in the gemset and available for use.
Troubleshooting
If your development environment is set up correctly, there should be no difficulty installing gems with the bundle install command. If your development environment is not set up correctly, you may see error messages when
Bundler attempts to install the Nokogiri gem. Nokogiri is often needed by other
gems (it is a dependency of some gems) and Nokogiri can become a problem to
install. Unlike most gems that are written in pure Ruby, parts of Nokogiri are
written in the C language and must be compiled using system tools that vary
with different operating systems. If you get an error while installing gems, and
the message says, An error occurred while installing nokogiri, ask for help
on Stack Overflow.
Git
Lets commit our changes to the Git repository and push to GitHub:
$ git add -A
$ git commit -m "add gems"
$ git push origin master
After your first use of git push origin master, you can use the shortcut
git push.
If you get a message:
GIT
85
fatal: Not a git repository (or any of the parent directories): .git
It indicates you are in a folder that has not been initialized with Git. You are
probably not in your project directory. Use the Unix command pwd to see
where you are.
If you get a message:
fatal: 'origin' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
It shows that you cant connect to GitHub to push the changes. To investigate,
enter:
$ git remote show origin
It is not absolutely necessary to use GitHub for this tutorial. Were only using
it so youll be familiar with the workflow of professional development.
Were ready to configure the application.
86
CHAPTER 9. GEMS
Chapter 10
Configure
Rails is known for its convention over configuration guiding principle. As
applied, the principle reduces the need for many configuration files. Its not
possible to eliminate all configuration files, however. Many applications require configuration of settings such as email account credentials or API keys
for external services.
In our tutorial application, well need to store an API key to access MailChimp,
which well use to add visitors email addresses to a mailing list. Well also
need to store credentials so we can send email using the SendGrid transactional
email service.
Rails provides the config/secrets.yml file for our configuration settings. Any
variable that is set in the config/secrets.yml file can be used elsewhere in our
Rails application, providing a single location for all our configuration variables.
Configuration Security
GitHub is a good place to store and share code. But when your repos are public,
they are not a good place for secret account credentials. In fact, any shared Git
repository, even a private repo, is a bad place to store email account credentials
87
88
If you see /bin/bash, thats great! If not, you may have to do some research
to find out how to set environment variables in your shell.
When you open a console window, the bash shell reads a configuration file in
your user home directory. You can use a Unix command to list all the files in
89
your user home directory (the ~ tilde character represents your home directory):
$ ls -1pa ~
.
.
.
.bash_profile
.bashrc
.
.
.
You should see either .bashrc or .bash_profile. Open either file and youll
likely find a command such as:
export PATH=~/.bin:$PATH
That is a command that sets the PATH environment variable. The command
might not be exactly the same but it is likely you will see some export commands.
If you dont have a .bashrc or .bash_profile file in your user home directory,
you can create one.
SENDGRID_USERNAME="example"
SENDGRID_PASSWORD="secret"
MAILCHIMP_API_KEY="Your_MailChimp_API_Key"
MAILCHIMP_LIST_ID="Your_List_ID"
OWNER_EMAIL="[email protected]"
90
The files .bashrc or .bash_profile are hidden in the file browser. You can use
Command-Shift-. (command shift dot) to show hidden files in the Mac OS
X file open dialog.
You should use quotes to surround configuration values (credentials) in the
.bashrc or .bash_profile files.
SendGrid
Youll need your SendGrid username and password. The credentials are the
same you use to sign in to the SendGrid website.
Add your SendGrid username and password to your .bashrc or .bash_profile
file:
export SENDGRID_USERNAME="example"
export SENDGRID_PASSWORD="secret"
MailChimp
When visitors sign up to receive a newsletter, well add them to a MailChimp
list. Add an environment variable for the MailChimp API key: MAILCHIMP_API_KEY.
Sign in to MailChimp to get your API key. Click your name at the top of the
navigation menu, then click Account. Click Extras, then API keys. You
have to generate an API key; MailChimp doesnt create one automatically.
export MAILCHIMP_API_KEY="Your_MailChimp_API_Key"
Youll need to create a MailChimp mailing list in preparation for our Mailing
List chapter. Have you already created a MailChimp mailing list? If not, the
91
MailChimp Lists page has a button for Create List. The list name and other
details are up to you.
Well need the MAILCHIMP_LIST_ID for the mailing list youve created. To
find the list ID, on the MailChimp Lists page, click the down arrow for a
menu and click Settings. At the bottom of the List Settings page, youll
find the unique ID for the mailing list.
export MAILCHIMP_LIST_ID="Your_List_ID"
Owner Email
Youll send email messages to this address when a visitor submits a contact
request form. Set OWNER_EMAIL with an email address where you receive
mail.
export OWNER_EMAIL="[email protected]"
Enter an email address and your environment variables will be set up with the
site owner email address.
Troubleshooting
Check that the SendGrid user name is set in your Unix environment variables:
$ echo "$SENDGRID_USERNAME"
You should see your SendGrid user name in the console response. Make sure
youve used underscores consistently and youve used SENDGRID_USERNAME
92
not SENDGRID_USER_NAME. Close and reopen your terminal to make sure the
environment includes any recent changes.
If youve set up Unix environment variables but echo "$SENDGRID_USERNAME"
doesnt return the correct variable in the console, you may have a problem with
the way youve set Unix environment variables. Most computers use the bash
shell and you can set environment variables in your .bashrc or .bash_profile
files. But not every system is alike. If it seems Unix environment variables are
not working, you may have to find a colleague who can help you troubleshoot.
You can continue with the tutorial and add the credentials directly to the config/secrets.yml file.
93
Be sure to use spaces, not tabs. Make sure there is a space after each colon
and before the value for each entry or you will get a message Internal Server
Error: mapping values are not allowed when you start the web server.
You used quotes to surround configuration values in the .bashrc or .bash_profile
files. Here, in the config/secrets.yml file, you dont need quotes when you are
importing Unix environment variables.
Domain Name
Well need a domain name when we configure email for delivery in production.
For development, use example.com. If you have your own domain name, you
can use that instead. Theres no need to keep the domain_name configuration
variable secret, so we dont need to set it in a Unix environment variable.
You can decide for yourself if the owner_email variable really needs to be
secret. Just for caution, Im suggesting you set it as a Unix environment variable.
94
Troubleshooting
Remember, in YAML files (with the file extension .yml), indentation is required
(your application will break without it).
Be sure to use spaces, not tabs. Make sure there is a space after each colon and
before the value for each entry.
If you have trouble setting Unix environment variables, you can add credentials
directly to the config/secrets.yml file. If you do so, you should not check the
file into Git until youve deleted the secrets from the file.
Replace the following:
development:
email_provider_username: <%= ENV["SENDGRID_USERNAME"] %>
email_provider_password: <%= ENV["SENDGRID_PASSWORD"] %>
with:
development:
email_provider_username: example
email_provider_password: 's#cr*t'
In a YAML file, you do not need quotes unless your string contains special
characters. If your password contains any of these characters you should surround the string with single quotes:
95
Again, DONT CHECK THE FILE INTO GIT if youve hardcoded your credentials directly in the config/secrets.yml file.
The article Rails Environment Variables shows alternatives to using Unix environment variables, if for any reason you cannot set environment variables on
your machine.
96
Configure Email
Email messages are visible in the console and the log file when you test the
application. If you dont want to actually send email, you can skip this step.
But its more fun when your application can actually send email.
You can learn more in the article Send Email with Rails.
CONFIGURE EMAIL
97
You can use port 25, 587, or 2525 (some ISPs restrict connections on port 25).
Its important to add these changes in the body of the configuration file, before
the end keyword. The order isnt important but dont add the configuration
statements after the end keyword.
Notice that we are using configuration variables that are set in the config/secrets.yml
file:
Rails.application.secrets.email_provider_username
98
Rails.application.secrets.email_provider_password
We could hard code a username and password here but that would expose
confidential data if your GitHub repository is public. Using configuration variables that are set in the config/secrets.yml file keeps your secrets safe.
Again, if you need to troubleshoot, you can enter the SendGrid username and
password directly in this file instead of the configuration variables. But for
security, dont commit to Git with the password hardcoded in this file.
This changes the configuration to send email when youre working on the application.
Make sure any code youve added to the config/environments/development.rb
file is placed before the final end keyword. If you add code after the final end
keyword, your application will fail with errors when you start the web server.
Later, after we add a contact form to the tutorial application, the application
will be ready to send email messages.
Git
Make sure youre in your application root directory.
GIT
Lets commit our changes to the Git repository and push to GitHub:
$ git add -A
$ git commit -m "add configuration"
$ git push
99
100
Chapter 11
101
102
<h1>Hello World</h1>
Refresh the browser window and youll see the Hello World message.
The Rails application server looks for any pages in the public folder by default.
If no filename is specified in the URL, the server will attempt to respond with a
file named index.html. This is a convention that dates to 1993; if no filename
was specified, one of the first web servers ever built (the NCSA httpd server)
would return a list of all files in the directory, unless a file named index.html
was present. Since then, index.html has been the default filename for a home
page.
Routing Error
What happens when no file matches the requested web address?
Enter the URL http://localhost:3000/about.html in your browser.
Youll see an error page that shows a routing error.
If you are using Cloud9 or Nitrous.io, add /about.html to the URL in the
preview browser window.
INTRODUCING ROUTES
103
Introducing TDD
With test-driven development, a developer tests behavior before implementing
a feature, expecting to see an error condition. Then the developer implements
the feature and sees a successful result to the test. Thats exactly what youve
done, in the simplest way.
Beginners tend to think TDD is scary and complicated. Now that youve experienced a simple form of TDD, maybe it wont be intimidating. Real TDD
means writing tests in Ruby before implementing features, but the principle is
the same.
Introducing Routes
The guiding principle of convention over configuration governs Rails routing. If the web browser requests a page named index.html, Rails will deliver
the page from the public folder by default. No configuration is required. But
what if you want to override the default behavior? Rails provides a configuration file to control web request routing.
Remove the public/index.html file:
$ rm public/index.html
If youve got only one terminal window open, youll have to stop the Rails
server with Control-c to get your terminal prompt. Here is where it is helpful
to have two terminal sessions going in different tabs.
Now lets set the About page as the home page.
104
Open the file config/routes.rb. Remove all the comments and replace the file
with this:
Rails.application.routes.draw do
root to: redirect('/about.html')
end
This snippet of Rails routing code takes any request to the application root
(http://localhost:3000/) and redirects it to the about.html file (which is expected to be found in the public folder).
There is no need to restart your application server to see the new behavior. If
you need to start the server:
$ rails server
Chapter 12
106
and the web browser, but even so, an initial request-response cycle delivers the
page that sets up the stream.
We can reduce the mystery of how the web works to its simplest components
when we investigate the request-response cycle. Well see that everything that
happens in a web application takes place within the flow of the request-response
cycle.
Lets look at the request-response cycle.
Developers use various web browsers during development. Ill provide instructions for Chrome, since it is the most popular. Even if you prefer Mozilla Firefox or Apple Safari, try this in Chrome, so you can follow along with the text.
Start our investigation by putting Chrome into Incognito Mode with CommandShift-N (on a Mac). On Linux, use Ctrl-Shift-N to get in incognito mode with
107
Chrome. Alternatively, you can clear the browser cache. This clears any files
that were previously cached by the browser.
The Developer Tools View is your primary diagnostic tool for front-end (browserbased) development, including CSS and JavaScript.
In Chrome on Mac OS X, press Command-Option-I to open the Developer
Tools View in a section of the browser window. Alternatively, you can find the
menu item under View/Developer/Developer Tools.
108
109
Heres the point of the exercise: The browsers diagnostic view shows all the
data exchanged between the browser and server. Youre looking at everything
that passes through the plumbing.
Notice how the diagnostic messages in the console window match the headers
in the browser diagnostic view. The browsers Request Method:GET matches
the servers Started GET. The browsers Request URL:http://localhost:3000/
matches the servers / for 127.0.0.1 (localhost is at IP address 127.0.0.1).
Notice there are no console log messages for pages delivered from the public
folder.
Soon well see much more in the console window, after weve built a dynamic
web page that is assembled by the application server.
110
111
In a Rails application, there is a single routing file, config/routes.rb, and multiple controllers, models, and views.
Considering the importance of the router, perhaps we should call our Rails
architecture the RCMV, or Routing-Controller-Model-View, pattern. Despite
the quibble about nomenclature, the architecture is well understood and used
by all Rails developers.
Heres the step-by-step walk-through of what happens.
When the web browser makes a request, a router component will check the
config/routes.rb file and determine which controller should handle the request,
based on the web address and HTTP protocol. The controller will obtain any
needed data from a model. After obtaining data, the controller will render a
response combining data from the model with a view component that provides
markup and layout. The response is an HTML file that the controller assembles
for the browser to display.
The model, view, and controller are files you create containing Ruby code.
Each file has a certain structure and syntax based on foundation model, view,
and controller classes defined in the Rails framework. The model, view, and
controller classes you create will inherit behavior from parent classes that are
part of the framework, so you will have less code to write yourself.
In most Rails applications, a model obtains data from a database, though some
models obtain data from a remote connection to another server. For example, a
User model might retrieve a user name and email address from a local database.
A User model could also obtain a users recent tweets from Twitter or a users
hometown from Facebook. The controller can obtain data from more than one
model if necessary.
A controller can have more than one action. For example, a User controller
might have actions to display a list of users, or add or delete a user from a
list. The config/routes.rb file matches a web request to a controller action. In
the software architects terminology, each action is a method of the controller
class. We use the terms action and method interchangeably when we talk about
a Rails controller; to be precise, controller actions are implemented as methods.
112
113
Earlier, we set up the config/routes.rb file. You can leave it in place. Well
change it in the next chapter.
Now well look at ways to implement the home page using the full power of
Rails.
114
115
116
Chapter 13
User Story
Well plan our work with a user story:
*Birthday Countdown*
As a visitor to the website
I want to see the owner's name
I want to see the owner's birthdate
I want to see how many days until the owner's next birthday
In order to send birthday greetings
This silly home page will help us explore Rails and learn about the Ruby language.
Our goal is to build a practical web application that you can really use. Later
well replace this silly home page with a useful web page that encourages visitors to sign up for a mailing list.
117
118
NAMING CONVENTIONS
119
Naming Conventions
Rails is picky about class names and filenames. Thats because of the convention over configuration principle. By requiring certain naming patterns, Rails
avoids complex configuration files.
Before we look at class and filename conventions, heres a note about typographic terminology:
a string is a sequence of characters
youre looking at an example of lowercase strings separated by spaces
(words!)
Titlecase means there is an Initial Capital Letter in a string
CamelCase contains a capital letter in the middle of a string
snake_case combines words with an underscore character instead of a
space
When you write code, youll follow rules for class names:
class Visitor - the model class name is capitalized and singular
120
Routing
Well create the route before we implement the model and controller.
Open the file config/routes.rb. Replace the contents with this:
Rails.application.routes.draw do
root to: 'visitors#new'
end
MODEL
121
Model
Most Rails models obtain data from a database. When you use a database,
you can use the rails generate model command to create a model that
inherits from the ActiveRecord class and knows how to connect to a database.
Our tutorial application doesnt need a database. Instead of inheriting from ActiveRecord, we create a Ruby class with methods that return the owners name,
birthdate, and days remaining until his birthday. This simple class provides an
easy introduction to Ruby code.
Create a file app/models/owner.rb:
122
class Owner
def name
name = 'Foobar Kadigan'
end
def birthdate
birthdate = Date.new(1990, 12, 22)
end
def countdown
today = Date.today
birthday = Date.new(today.year, birthdate.month, birthdate.day)
if birthday > today
countdown = (birthday - today).to_i
else
countdown = (birthday.next_year - today).to_i
end
end
end
This is your first close look at Ruby code. The oddest thing youll see is the
owners name, Foobar Kadigan. Everything else will make sense with a bit
of explanation.
Keep in mind that we are using a text file to create an abstraction that we can
manipulate in the computers memory. Software architects call these abstractions objects. In Ruby, everything we create and manipulate is an object. To
distinguish one object from another, we define it as a class, give it a class name,
and add behavior in the form of methods.
The first line class Owner defines the class and assigns a name. At the very
end of the file, the end keyword completes the class definition.
We define three methods, starting with def (for method definition) and ending with end.
def name . . . end
def birthdate . . . end
MODEL
123
124
Note that Ruby has specific expectations about the syntax of numbers. The
Date.new(...) method expects integers. Imagine a September birthday.
You must use Date.new(1990, 9, 22). If you enter a date in the format
Date.new(1990, 09, 22), youll get a syntax error Invalid octal digit
when you test the application. Ruby expects numbers that begin with zero to
be octal numbers; youll get an error because octal numbers cant contain the
digit 9.
Our countdown method contains the most complex code in the class.
First, we set a variable today with todays date. The Date.today method creates an object that represents the current date. When the Date.today method
is called, Ruby gets the current date from the computers system clock.
Next we create a birthday variable and assign a new date that combines todays year with the month and day of the birthdate. This gives us the date
of Foobar Kadigans birthday this year.
The Date class can perform complex calendar arithmetic. The variables birthdate
and today are instances of the Date class. We can use a greater-than operator
to determine if Foobar Kadigans birthday is in the future or the past.
The if ... else ... end structure is a conditional statement. If the
birthday is in the future, we subtract today from birthday to calculate the
number of days remaining until the owners birthday, which we assign to the
countdown variable.
If the birthday has already passed, we apply a next_year method to the birthday to get next years birthday. Then we subtract today from birthday.next_year
to calculate the number of days remaining until the owners birthday, which we
assign to the countdown variable.
The result might be fractional so we use the utility method to_i to convert
the result to a whole number (integer) before assigning it to the countdown
variable.
This shows you the power of programming in Ruby. Notice that I needed 16
paragraphs and over 600 words to explain 15 short lines of code. We used only
VIEW
125
seven Ruby abstractions but they represent thousands of lines of code in the
Ruby language implementation. With knowledge of Ruby syntax and the Ruby
API, a few short lines of code in a text file gives us amazing ability.
In an upcoming chapter, well look more closely at the syntax and keywords of
the Ruby language. But without knowing more than this, we can build a simple
web application.
Lets see how we can put this functionality to use on a web page.
View
The Owner model provides the data we want to see on the Home page.
Well create the markup and layout in a View file and add variables that present
the data.
View files go in folders in the app/views/ directory. In a typical application,
one controller can render multiple views, so we make a folder to match each
controller. You can make a new folder using your file browser or text editor. Or
use the Unix mkdir command:
$ mkdir app/views/visitors
Weve created a visitors/ folder within the app/views/ directory. We have only
a single new view but if we had more views associated with the Visitors controller, theyd go in the app/views/visitors/ folder.
126
We name our View file new.html.erb, adding the .erb file extension so that
Rails will use the ERB templating engine to interpret the markup.
There are several syntaxes that can be used for a view file. In this tutorial, well
use the ERB syntax that is most commonly used by beginners. Some experienced developers prefer to add gems that provide the Haml or Slim templating
engines. As you might guess, a View that uses the Haml templating syntax
would be named new.html.haml.
Our HTML markup is minimal, using only the <h3> and <p> tags. The only
ERB markup we add are the <%= ... %> delimiters. This markup allows us
to insert Ruby code which will be replaced by the result of evaluating the code.
In other words, <%= @owner.name %> will appear on the page as Foobar
Kadigan.
You may have noticed that we refer to the Owner model with the variable
@owner. It will be clear when we create the Visitors controller why we use
this syntax (a variable name that begins with the @ character is called an instance variable).
Obviously, if all we wanted to do was include the owners name on the page,
it would be easier to simply write the text. The Rails implementation becomes
useful if the name is retrieved from a database or created programmatically.
We can better see the usefulness of the Owner model when we look at the use of
<= @owner.countdown %>. There is no way to display a calculation using
only static HTML, so Rails gives us a way to display the birthday countdown
calculation.
If youre a programmer, you might wonder why we only output the variable on
the page. Since we can use ERB to embed any Ruby code, we could perform
the calculation right on the page by embedding <%= (Date.new(today.year,
@owner.birthdate.month, @owner.birthdate.day) - Date.today).to_i
%>. If youve used JavaScript or PHP, you may have performed calculations
like this, right on the page. Rails would allow us to do so, but the practice
violates the separation of concerns principle that encourages us to perform
complex calculations in a model and only display data in the view.
CONTROLLER
127
Before we can display the home page, we need to create the Visitors controller.
Controller
The Visitors controller is the glue that binds the Owner model with the VisitorsController#new view.
Note: When we refer to a controller action, we use the notation VisitorsController#new, joining the controller class name with the action (method) that
renders a page. In this context, the # character is only a documentation convention.
Note: VisitorsController will be the class name and visitors_controller.rb
will be the filename. The class name is written in camelCase (with a hump in
the middle, like a camel) so we can combine two words without a space.
Unix commands get messy when filenames include spaces so we create a filename that combines two words with an underscore (sometimes called snake_case).
Create a file app/controllers/visitors_controller.rb:
class VisitorsController < ApplicationController
def new
@owner = Owner.new
end
end
128
If we dont instantiate the Owner model, well get an error when the controller
new action attempts to render the view because we use the @owner instance in
the view file.
Keep in mind the purpose of the controller. Each controller action (method)
responds to a request by obtaining a model (if data is needed) and rendering a
view.
Youve already created a view file in the app/views/visitors folder. The new action of the VisitorsController renders the template app/views/visitors/new.html.erb.
The new method is deceptively simple. Hidden behavior inherited from the
ApplicationController does all the work of rendering the view. We can make
the hidden code explicit if we wish to. It would look something like this:
class VisitorsController < ApplicationController
def new
@owner = Owner.new
render 'visitors/new'
end
end
SCAFFOLDING
129
find how it works. As you gain experience, you can dive into the Rails source
code to unravel the magic.
Scaffolding
This tutorial aims to give you a solid foundation in basic concepts. The model
viewcontroller pattern is one of the most important. Ive found the best way
to understand modelviewcontroller architecture is to create and examine the
model, view, and controller files.
As you continue your study of Rails, youll find other tutorials that use the
scaffolding shortcut. For example, Rails Guides: Getting Started with Rails
includes a section Getting Up and Running Quickly with Scaffolding which
shows how to use the rails generate scaffold command to create model,
view, and controller files in a single operation. Students often use scaffolding
to create simple Rails applications.
In practice, Ive observed that working Rails developers seldom use scaffolding. Theres nothing wrong with it; it just seems that scaffolding doesnt offer
much that cant be done as quickly by hand.
130
Git
At this point, you might have the Rails server running in your console window.
Were going to run a git command in the console now.
You might think you have to enter Control-c to shut down the server and get
the command prompt. But thats not necessary. You can open more than one
console window. Your terminal application lets you open multiple tabs so you
can easily switch between windows without using a lot of screen real estate.
GIT
131
If you havent tried it, now is a good time. It is convenient to have a console
window open for the server and another for various Unix commands.
Lets commit our changes to the Git repository and push to GitHub:
$ git add -A
$ git commit -m "dynamic home page"
$ git push
132
Chapter 14
Troubleshoot
In the last chapter, we built a dynamic home page and learned about the model
viewcontroller architecture of Rails. There was a lot to learn, but the code was
simple, and I hope it worked the first time you tried it.
Before we do any more work on our tutorial application, we need to learn about
troubleshooting and debugging. As a software developer, youll spend a lot of
time with code that doesnt work. Youll need tools and techniques to diagnose
problems.
Git
In this chapter well make changes to the application just for troubleshooting.
Before you get started, make sure the work youve done is committed to your
git repository. Use the git status command to check:
$ git status
134
On branch master
nothing to commit, working directory clean
If git status reports any uncommitted changes, go back to the last step in
the previous chapter and commit your work to the git repository before continuing. At the end of this chapter, were going to throw away the work weve
done in this chapter. We dont want to accidentally throw away work from the
previous chapter so make sure it is committed to the repository.
The command irb launches the program and displays a prompt that show your
Ruby version, a line number, and an arrow. If you enter a valid Ruby expression, the interpreter will display the result of evaluating the expression.
Try simple arithmetic:
>> n = 2
=> 2
>> n + 2
=> 4
135
Wow! You are using your computer for simple math. Maybe you can delete
the calculator app from your phone.
IRB will evaluate any Ruby expression and helps you quickly determine if
syntax and logic is correct.
Quitting IRB
It can be very frustrating to find you are stuck inside IRB. Unlike most shell
commands, you cant quit with Control-c. Enter Control-d or type exit to quit
136
IRB:
$ irb
>> exit
Beyond IRB
If you ask experienced Rails developers for help with IRB, theyll often recommend you switch to Pry. Pry is a powerful alternative to the standard IRB shell
for Ruby. As you gain experience, you might take a look at Pry to see what
the enthusiasm is all about. But for now, as a beginner trying out a few lines of
Ruby code, theres no need to learn Pry.
Rails Console
IRB only evaluates expressions that are defined in the Ruby API. IRB doesnt
know Rails.
Itd be great to have a tool like IRB that evaluates any expression defined in the
Rails API. The tool exists; its called the Rails console. It is particularly useful
RAILS CONSOLE
137
because it loads your entire Rails application. Your application will be running
as if the application was waiting to respond to a web request. Then you can
expose behavior of any pieces of the web application.
$ rails console
...
Loading development environment (Rails 4.x.x)
>>
The Rails console behaves like IRB but loads your Rails development environment. The prompt shows it is ready to evaluate an expression.
Lets use the Rails console to examine our Owner model:
>> myboss = Owner.new
=> #<Owner:0x007fc18e91faf8>
Weve created a variable named myboss and created a new instance of the
Owner class. The Rails console responds by displaying the unique identifier it
uses to track the object. The identifier is not particularly useful, except to show
that something was created.
If youre unsure about the difference between an instance and a class, weve
just seen that we can make one or more instances of a class by calling the
Owner.new method. When we specify the Owner class, the class definition is
loaded into the computers working memory (our development environment)
from the class definition file on disk. Then we can use the Owner.new method
to make one or more instances of the Owner class. Each instance is a unique
object with its own data attributes but the same behavior as other objects instantiated from its class.
Lets assign the name of our boss to a variable called name:
>> name = myboss.name
=> "Foobar Kadigan"
138
Were done for now. When we quit the Rails console or shut down the computer
the Owner class definition remains stored on disk but the instances disappear.
The bits that were organized to create the variable name will evaporate into the
ether.
Actually, the bits are still there, in the form of logic states in the computers
chips, but they have no meaning until another program uses them.
Enter Control-d or type exit to quit the Rails console.
The Rails console is a useful utility. It is like a handy calculator for your code.
Use it when you need to experiment or try out short code snippets.
Rails Logger
As you know, a Rails application sends output to the browser that makes a
web request. On every request, it also sends diagnostic output to the server
log file. Depending on whether the application is running in the development
environment or in production, the log file is here:
log/development.log
log/production.log
In development, everything written to the log file appears in the console window after you run the rails server command. Scrolling the console window is a good way to see diagnostics for every request.
RAILS LOGGER
139
Heres what you see in the log after you visit the application home page:
Started GET "/" for 127.0.0.1 at ...
Processing by VisitorsController#new as HTML
Rendered visitors/new.html.erb within layouts/application (48.8ms)
Completed 200 OK in 233ms (Views: 211.5ms | ActiveRecord: 0.0ms)
You may have more than one console window open in the terminal application.
If you dont see your log output in your terminal, check if you have tabs with
other windows.
Heres the best part. You can add your own messages to the log output by using
the Rails logger. Lets try it out.
Modify the file app/controllers/visitors_controller.rb:
class VisitorsController < ApplicationController
def new
Rails.logger.debug 'DEBUG: entering new method'
@owner = Owner.new
Rails.logger.debug 'DEBUG: Owner name is ' + @owner.name
end
end
Visit the home page again and youll see this in the console output:
Started GET "/" for 127.0.0.1 at ...
Processing by VisitorsController#new as HTML
DEBUG: entering new method
DEBUG: Owner name is Foobar Kadigan
Rendered visitors/new.html.erb within layouts/application (0.2ms)
Completed 200 OK in 8ms (Views: 4.6ms | ActiveRecord: 0.0ms)
If you really needed to do so, you could add a logger statement at every step in
the application. You could see how the application behaves, step by step. And
you could print the value of every variable at every step. Youll never need
140
diagnostics at this level of detail in Rails, but the logger is extremely useful
when you are trying to understand unexpected behavior.
Lets add logger statements to the Owner model. Modify the file app/models/owner.rb:
class Owner
def name
name = 'Foobar Kadigan'
end
def birthdate
birthdate = Date.new(1990, 12, 22)
end
def countdown
Rails.logger.debug 'DEBUG: entering Owner countdown method'
today = Date.today
birthday = Date.new(today.year, birthdate.month, birthdate.day)
if birthday > today
countdown = (birthday - today).to_i
else
countdown = (birthday.next_year - today).to_i
end
end
end
Youll often need to get inside the model or controller to see whats happening. The Rails logger is the best tool for the job.
141
For more about the Rails logger, see the RailsGuide: Debugging Rails Applications.
Notice how the diagnostic messages in the console window match the headers
in the browser diagnostic view. The browsers Request Method:GET matches
142
143
end
144
To save space, Im only showing the top line of the stack trace. Ive eliminated
about sixty lines from the stack trace.
Dont feel bad if your reaction to a stack trace is an immediate, TMI! Indeed,
it is usually Too Much Information. There are times when it pays to carefully
read through the stack trace line by line, but most often, only the top line of the
stack trace is important.
In this case, both the error page and the top line of the stack trace show the
application failed (barfed) when it encountered an uninitialized constant
at line 7 of the app/controllers/visitors_controller.rb file in the new method.
Its easy to find line 7 in the file and see that is exactly where we added a string
that Rails doesnt understand.
The point of this exercise is to encourage you to read the top line of the stack
trace and use it to diagnose the problem. Im always surprised how many developers ignore the stack trace, probably because it looks intimidating.
Raising an Exception
As you just saw, you can purposefully break your application by adding characters that Rails doesnt understand. However, there is a better way to force
your program to halt, called raising an exception.
Lets try it. Modify the file app/controllers/visitors_controller.rb:
RAISING AN EXCEPTION
145
You can throw an error by using the raise keyword from the Ruby API. You
can provide any error message youd like in quotes following raise.
Heres the console log after you try to visit the home page:
Started GET "/" for 127.0.0.1 at ...
Processing by VisitorsController#new as HTML
DEBUG: entering new method
DEBUG: Owner name is Foobar Kadigan
Completed 500 Internal Server Error in 22ms
RuntimeError - Deliberate Failure:
app/controllers/visitors_controller.rb:7:in `new'
.
.
.
Before we continue, lets remove the deliberate failure. Modify the file app/controllers/visit
class VisitorsController < ApplicationController
def new
Rails.logger.debug 'DEBUG: entering new method'
@owner = Owner.new
Rails.logger.debug 'DEBUG: Owner name is ' + @owner.name
end
end
Rails and the Ruby API provide a rich library of classes and methods to raise
and handle exceptions. For example, you might want to display an error if a
146
user enters a birthdate that is not in the past. Rails includes various exception
handlers to display errors in production so users will see a helpful web page
explaining the error.
Git
Theres no need to save any of the changes we made for troubleshooting.
You could go to each file and carefully remove the debugging code you added.
But theres an easier way.
Check which files have changed:
$ git status
# Changes not staged for commit:
#
(use "git add ..." to update what will be committed)
#
(use "git checkout -- ..." to discard changes in working directory)
#
# modified:
app/controllers/visitors_controller.rb
# modified:
app/models/owner.rb
#
no changes added to commit (use "git add" and/or "git commit -a")
The Git command git reset -hard HEAD discards any changes youve
made since the most recent commit. Check the status to make sure:
$ git status
# On branch master
nothing to commit, working directory clean
Chapter 15
148
to recognize the language keywords and use the correct syntax when you type
Ruby code in your text editor.
To that end, this chapter will review the Ruby keywords and syntax youve
already learned. And youll extend your knowledge so youll be prepared for
the Ruby youll encounter in upcoming chapters.
Ruby Example
To improve your reading knowledge of Ruby, well work with an example file
that contains a variety of Ruby expressions.
We wont use this file in our tutorial application, so youll delete it at the end of
this chapter. But well approach it as real Ruby code, so make a file and copy
the code using your text editor.
First we have to consider where the file should go. It will not be a model,
view, controller, or any other standard component of Rails. Rails has a place
for miscellaneous files that dont fit in the Rails API. Well create the file in
the lib/ folder. Thats the folder youll use for any supporting Ruby code that
doesnt fit elsewhere in the Rails framework.
Create a file lib/example.rb:
class Example < Object
# This is a comment.
attr_accessor :honorific
attr_accessor :name
attr_accessor :date
def initialize(name,date)
@name = name
@date = date.nil? ? Date.today : date
end
def backwards_name
@name.reverse
end
RUBY EXAMPLE
def to_s
@name
end
def titled_name
@honorific ||= 'Esteemed'
titled_name = "#{@honorific} #{@name}"
end
def december_birthdays
born_in_december = [ ]
famous_birthdays.each do |name, date|
if date.month == 12
born_in_december << name
end
end
born_in_december
end
private
def famous_birthdays
birthdays = {
'Ludwig van Beethoven' => Date.new(1770,12,16),
'Dave Brubeck' => Date.new(1920,12,6),
'Buddy Holly' => Date.new(1936,9,7),
'Keith Richards' => Date.new(1943,12,18)
}
end
end
In some ways, this Ruby code is like a poem from Lewis Carroll:
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe;
All mimsy were the borogoves,
And the mome raths outgrabe.
"Beware the Jabberwock, my son!
The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
The frumious Bandersnatch!"
149
150
The code follows the rules of Ruby syntax, and unlike the poem, uses meaningful words. But it is unclear how the author intends anyone to use the code.
If youre beginning a career as a Rails developer, this wont be the last time
you look at code and wonder what the author was intending. In this case, I just
want to give you some code that illustrates typical Ruby syntax and structure.
Ruby Keywords
When reading Ruby code, the first challenge is determining which words are
Ruby keywords and which were made up by the developer. Code is only strings
of characters. But some strings have special meaning for everyone and all
others are arbitrary words that only have meaning to an individual developer.
As you gain experience, youll recognize Ruby keywords because youve seen
them before.
Youll also recognize a developers made-up words because of their position
relative to other words and symbols. Some made-up words will be obvious
because they are just too idiosyncratic to be part of the Ruby language. For
example, youll rightly guess that myapp or fluffycat are not part of the
Ruby language.
If youre reading a Lewis Carroll poem, you could look up words in a dictionary
to see if you find them.
There is only one way to be sure which words are part of the Ruby language:
Check the Ruby API.
As an exercise, pick one of the words from the example code that you think
might be a Ruby keyword and search the API to find it.
If you want to be a diligent student, you can check every keyword in the example code to find out whether it is in the Ruby API. It is more practical to learn
to recognize Ruby keywords, which well do next.
RUBY FILES
151
API Documentation
The Ruby API documentation lists every keyword in the language:
ruby-doc.org - the official Ruby API
apidock.com/ruby - Ruby API docs with usage notes
Ruby Files
When we write code, we save it in files. Weve added our miscellaneous example file to the lib/ folder.
By convention, Ruby files end with the file extension .rb.
Using IRB
In the Troubleshooting chapter, you used IRB (the Interactive Ruby Shell) to
try out Ruby code. You can use IRB to try out the example code in the console.
$ irb
>> load 'lib/example.rb'
=> true
>> require 'date'
=> true
>> ex = Example.new('Daniel',nil)
=> #<Example:0x007fb46c9eecd8 @name="Daniel", @date=#<Date: 2015-12-23 ...
>> list = ex.december_birthdays
=> ["Ludwig van Beethoven", "Dave Brubeck", "Keith Richards"]
>>
Entering the load directive and the filename brings the code into IRB.
The require date statement loads the Ruby date library.
152
Comments
Ruby ignores everything that is marked as a comment. Use comments for notes
to yourself or other programmers.
# This is a comment.
You can also turn code into comments if you dont want the code to run. This
is a common trick when you want to turn off some part of your code but you
dont want to delete it just yet, because you are trying out alternatives.
153
Assignment
In Ruby, like many other programming languages, the equals sign indicates we
are assigning a value.
name = 'Foobar Kadigan'
154
Variables can be assigned strings of letters, numbers, or anything else. Anything else is very broad because we can use Ruby to make complex structures
that contain data and also do work. These complex structures are objects and
we say that Ruby is object-oriented because it is easy to work with objects in
Ruby.
Object-Oriented Terminology
Software architects use a common vocabulary to talk about programming languages:
class
instance or object
method
attribute or property
inheritance
class hierarchy
OBJECT-ORIENTED TERMINOLOGY
155
There are three ways to learn what these words mean. You can memorize the
definitions. You can write code and intuitively grasp the meanings. Or you can
gain an understanding by applying metaphors.
Houses
For example, some programming textbooks attempt to explain a class like this:
A blueprint for a house design is like a class definition. All the houses built
from that blueprint are objects of a class we could call House.
Vehicles
Or: The concept of vehicle is like a class. Vehicles can have attributes, like
color or number of doors. They have behavior, or methods, like buttons that
turn on lights or honk a horn. The concepts of truck or car are also classes,
inheriting common characteristics from a superclass vehicle. The blue car
in your driveway with four doors is an object, a particular instance of the class
car.
Cookies
I like the cookie metaphor the best.
A class definition is like a cookie cutter.
Bits in the computer memory are like cookie dough.
The cookie cutter makes as many individual cookies as you want. Each cookie
is an instance of the Cookie class, with the same shape and size as the others.
Cookies are objects.
You can decorate each cookie with sprinkles, which are attributes that are
unique to each instance. Some cookies get red sprinkles, some get green, but
156
Limitations of Metaphors
Metaphors are imperfect.
If baking was like running a program, all the cookies would disappear as soon
as the oven was turned off.
When a software program contains a car model, it doesnt fully model cars in
the physical world. It represents an abstraction of characteristics a programmer
deems significant. Lets make a model for a Person that contains an attribute
Gender. What values are possible for the attribute Gender? For many years,
Facebook offered two choices, male and female. In 2014, Facebook suddenly
offered a choice of over fifty gender terms. As Sarah Mei discusses in a blog
post, Why Gender is a Text Field on Diaspora, your assumptions have consequences when you build a model.
Most classes in software APIs dont model anything in the real world. They
typically represent an abstraction, like an Array or a Hash, which inherits characteristics from another abstraction, for example, a Collection.
Given the limitations of metaphors, maybe it is easier to simply say that software allows us to create abstractions that are made real and then manipulated
and transformed. Terminology such as class and instance describe the abstractions and the relationships among them.
CLASSES
157
Definitions
Here are definitions for some of the terms we encounter when we consider
Rails from the perspective of a software architect:
class an abstraction that encapsulates data and behavior
class definition written code that describes a class
instance or object a unique copy of a class that exists only while a program is
running
inheritance a way to make a class by borrowing from another class
class hierarchy classes that are related by inheritance
method a command that returns data from an object
attribute or property data that can be set or retrieved from the object
variable a name that can be assigned a value or object
expression or statement any combination of variables, classes, and methods
that returns a result
Some of these terms are abstractions that are made real in the Ruby API
(such as class and method); others are just terms that describe code, much like
we use terms such as adjective or noun to talk about the grammar of the
English spoken language.
Classes
You dont have to create classes to program in Ruby. If you only write simple
programs, you wont need classes. Classes are used to organize your code and
158
make your software more modular. For the software architect, classes make
it possible to create a structure for complex software programs. To use Rails,
youll use the classes and methods that are defined in the Rails API.
There is one class at the apex of the Ruby class hierarchy: BasicObject.
BasicObject is a very simple class, with almost no methods of its own. The
Object class inherits from BasicObject. All classes in the Ruby and Rails
APIs inherit behavior from Object. Object provides basic methods such as
nil? and to_s (to string) for every class that inherits from Object.
We create a class Example and inherit from Object with the < inherits from
operator:
class Example < Object
.
.
.
end
The end statement indicates all the preceding code is part of the Example
class.
In Ruby, all classes inherit from the Object class, so we dont need to explicitly
subclass from Object as we do here. The example just shows it for teaching
purposes.
Here is the Example class without the explicit subclassing from Object:
class Example
.
.
.
end
Much of the art of programming is knowing what classes are available in the
API and deciding when to subclass to inherit useful methods.
METHODS
159
Methods
Classes give organization and structure to a program. Methods get the work
done.
Any class can have methods. Methods are a series of expressions that return a
result (a value). We say methods describe the class behavior.
A method definition begins with the keyword def and (predictably) ends with
end.
def backwards_name
@name.reverse
end
Here we are overriding the to_s (to string) method from the parent Object
class.
Ordinarily, the to_s method returns the objects class name and an object id.
Here we will return the string assigned to the variable @name.
Most times you wont override the to_s (to string) method. This example
shows how you can override any method inherited from a parent class.
160
Dot Operator
The dot is the method operator. This tiny punctuation symbol is a powerful
operator in Ruby.
It allows us to call a method to get a result.
Sometimes we say we send a message to the object when we invoke a method,
implying the object will send a result.
Some classes, such as Date, provide class methods which can be called directly
on the class without instantiating it first. For example, you can run this in the
Rails console:
Date.today
=> Tue, 15 Oct 2013
More often, methods are called on variables which are instances of a class. For
example:
birthdate = Date.new(1990, 12, 22)
=> Sat, 22 Dec 1990
birthmonth = birthdate.month
=> 12
We can apply method chaining to objects. For example, String has methods
reverse and upcase (among many others). We could write:
nonsense = 'foobar'
=> "foobar"
reversed = nonsense.reverse
=> "raboof"
capitalized = reversed.upcase
=> "RABOOF"
INITIALIZE METHOD
161
'foobar'.reverse.upcase
=> "RABOOF"
Classes create a structure for our software programs and methods do all the
work.
Initialize Method
Objects are created from classes before they are used. As I suggested earlier,
class definitions are cookie cutters; the Ruby interpreter uses them to cut cookies. When we call the new method, we press the cookie cutter into the dough
and get a new object. All the cookies will have the same shape but they can be
decorated differently, by sprinkling attributes of different values.
The initialize method is one of the ways we sprinkle attributes on our
cookie.
162
def initialize(name,date)
Method Parameters
Methods are useful when they operate on data.
If we want to send data to a method, we define the method and indicate it will
accept parameters. Parameters are placeholders for data values. The values
that are passed to a method are arguments. Parameters are empty placeholders and arguments are the actual values. In practice, parameters and
arguments are terms that are used interchangeably and not many developers
will notice if you mix up the terms.
Our initialize method takes name and date arguments:
def initialize(name,date)
Ruby is clever with method parameters. You can define a method and specify
default values for parameters. You can also pass extra arguments to a method
if you define a method that allows optional parameters. This makes methods
very flexible.
We separate our parameters with commas. For readability, we enclose our list
of parameters in parentheses. In Ruby, parentheses are always optional but they
often improve readability.
VARIABLE
163
Variable
In Ruby, everything is an object. We can assign any object to a variable. The
variable works like an alias. We can use a variable anywhere as if it were the
assigned object. The variable can be assigned a string, a numeric value, or an
instance of any class (all are objects).
name
You can assign a new value to a variable anywhere in your method. You can
assign a different kind of object if you want. You can take away someones
name and give them a number. We can create a variable player, assign it the
string Jackie Robinson, replace the value with an integer 42, or even a
date such as Date.new(1947,4,15).
Symbol
Obviously, we see many symbols when we read Ruby code, such as punctuation marks and alphanumeric characters. But symbol has a specific meaning in
Ruby. It is like a variable, but it can only be assigned a value once. After the
initial assignment, it is immutable; it cannot be changed.
You will recognize a symbol by the colon that is always the first character.
:name
Symbols are efficient and fast because the Ruby interpreter doesnt have to
work to check their current values.
Youll often see symbols used in Rails where you might expect a variable.
164
Attributes
In an object, methods do the work and data is stored as variables. We can use
the initialize method to input data to the object. We cant access data in
variables from outside the object unless it is exposed as attributes.
Classes can have attributes, which we can set and get. That is, we can establish a value for an attribute and retrieve the value by specifying the attribute
name.
Attributes are a convenient way to push data to an object and pull it out later.
In Ruby, attributes are also called properties.
Here we use the attr_accessor directive to specify that we want to expose
honorific, name and date attributes.
attr_accessor :honorific
attr_accessor :name
attr_accessor :date
In Ruby, attributes are just specialized methods that expose data outside the
object.
Instance Variable
Inside an object, an ordinary variable only can be used within the method in
which it appears. If you use a variable with the same name in two different
INSTANCE VARIABLE
165
methods, it will have a different value in each method. The scope of a variable
is limited to the method in which it is used.
Often you want a variable to be available throughout an instance, within any
method. You can declare an instance variable by using an @ (at) sign as the
first character of the variable name.
The instance variable can be used by any method after the class is instantiated.
@name = name
The values assigned to instance variables are unique for every instance of the
class. If you create multiple instances of a class, each will have its own values
for its instance variables. Here we create two instances of the Example class.
The @name instance variable will be Daniel in the first instance and Foobar
in the second instance.
ex1 = Example.new('Daniel',nil)
ex2 = Example.new('Foobar',nil)
An instance variable is not visible outside the object in which it appears; but
when you create an attr_accessor, it creates an instance variable and makes
it accessible outside the object.
166
learn that the variable with the @ (at) sign is called an instance variable and is
only available within the scope of the instance (practically speaking, to other
methods in the class definition). That leads to a question: Why is an instance
variable available inside a view?
There is a good reason. A Rails view is NOT a separate class. It is a template and, under the hood, it is part of the current controller object. From the
viewpoint of a programmer, a Rails controller and a view are separate files,
segregated in separate folders. From the viewpoint of a software architect, the
controller is a single object that evaluates the template code, so an instance
variable can be used in the view file.
This example shows us that the programmer and the software architect have
different perspectives on a Rails application. Understanding Rails requires an
integration of multiple points of view.
CONDITIONAL
167
if not x
x = y
end
Conditional
Conditional logic is fundamental to programming. Our code is always a path
with many branches.
When the Ruby interpreter encounters an if keyword, it expects to find an
expression which evaluates as true or false (a boolean).
If the expression is true, the statements following the condition are executed.
If the expression is false, any statements are ignored, unless there is an else,
in which case an alternative is executed.
if date.month == 12
.
.
.
end
168
Ternary Operator
A basic conditional structure might look like this:
if date.nil?
@date = Date.today
else
@date = date
end
We test if date is undefined (nil). If nil, we assign todays date to the instance
variable @date. If date is already assigned a value, we assign it to the instance
variable @date. This is useful in the initialize(name,date) method in
our example code because we want to set todays date as the default value for
the instance variable @date if the parameter date is nil.
Ruby developers like to keep their code tight and compact. So youll see a condensed version of this conditional structure often, particularly when a default
value must be assigned.
This compact conditional syntax is named the ternary operator because it has
three components. Here is the syntax:
condition ? value_if_true : value_if_false
This is another example of Ruby syntax that you must learn to recognize by
sight because it is difficult to interpret if you have never seen it before.
For more Ruby code that has been condensed into obscurity, see an article on
Ruby Golf. Ruby golf is the sport of writing code that uses as few characters
as possible.
INTERPOLATION
169
Interpolation
Rubyists love to find special uses for orthography such as hashmarks and curly
braces. It seems Rubyists feel sorry for punctuation marks that dont get much
use in the English language and like to give them new jobs.
We already know that we can assign a string to a variable:
name = 'Foobar Kadigan'
Single quote marks indicate a string. In the example above, we enclose a space
character within quote marks so we add a space to our string.
You can eliminate the ungainly mix of plus signs, single quote marks, and space
characters in the example above.
Use double quote marks and you can perform interpolation, which gives a new
job to the hashmark and curly brace characters:
@honorific = 'Mr.'
@name = 'Foobar Kadigan'
titled_name = "#{@honorific} #{@name}"
=> "Mr. Foobar Kadigan"
The hashmark indicates any expression within the curly braces is to be evaluated and returned as a string. This only works when you surround the expression with double quote marks.
170
Interpolation is cryptic when you first encounter the syntax, but it streamlines
string concatenation.
Access Control
Any method you define will return a result.
Sometimes you want to create a method that only can be used by other methods
in the same class. This is common when you need a simple utility method that
is used by several other methods.
Any methods that follow the keyword private should only be used by methods in the same class (or a subclass).
private
You often see private methods in Rails. Ruby provides a protected keyword as
well, but the difference between protected and private is subtle and protected
is seldom seen in Rails applications.
Hash
Our example code includes a private method named famous_birthdays that
returns a collection of names and birthdays of famous musicians.
Computers have always been calculation machines; they are just as important
in managing collections.
One important type of collection is named a Hash. A Hash is a data structure
that associates a key to some value. You retrieve the value based upon its key.
This construct is called a dictionary, an associative array, or a map in other
languages. You use the key to look up a value, as you would look up a
definition for a word in a dictionary.
HASH
171
Youll recognize a Hash when you see curly braces (again, Rubyists give a job
to under-utilized punctuation marks).
birthdays = {
'Ludwig van Beethoven' => Date.new(1770,12,16),
'Dave Brubeck' => Date.new(1920,12,6),
'Buddy Holly' => Date.new(1936,9,7),
'Keith Richards' => Date.new(1943,12,18)
}
Rubyists also like to create novel uses for mathematical symbols. The combination of an = (equals) sign and > (greater than) sign is called a hashrocket.
The => (hashrocket) operator associates a key and value pair in a Hash.
Ruby 1.9 introduced a new way to associate key and value pairs in a Hash:
birthdays = {
beethoven: Date.new(1770,12,16),
brubeck: Date.new(1920,12,6),
holly: Date.new(1936,9,7),
richards: Date.new(1943,12,18)
}
Here, instead of using a string as the key, we are using Ruby symbols, which
enable faster processing. The : (colon) character associates the key and value.
Ordinarily, a symbol is defined with a leading colon character. In a Hash, a
trailing colon makes a string into a symbol.
If you want to transform a string containing spaces into a symbol in a Hash,
you can do it, though the syntax is awkward:
birthdays = {
:'Ludwig van Beethoven' => Date.new(1770,12,16)
}
Whether with colons or hashrockets, youll often see Hashes used in Rails.
172
Array
An Array is a list. Arrays can hold objects of any data type. In fact, arrays can
contain a mix of different objects. For example, an array can contain a string
and another array (this is an example of a nested array).
An array can be instantiated with square brackets:
born_in_december = [ ]
If we dont want to use quote marks and commas to separate strings in a list,
we can use the %w syntax:
my_list = %w( apples oranges )
In our example code, we use the << shovel operator to add items to the array:
ITERATOR
173
Iterator
Of all the methods available for a Ruby collection such as Hash or Array, the
iterator may be the most useful.
Youll recognize an iterator when you see the each method applied to a Hash
or Array:
famous_birthdays.each
The each keyword is always followed by a block of code. Each item in an Array, or key-value pair in a Hash, is passed to the block of code to be processed.
Block
You can recognize a block in Ruby when you see a do ... end structure. A
block is a common way to process each item when an iterator such as each is
applied to a Hash or Array.
In our example, we iterate over the famous_birthdays hash:
famous_birthdays.each do |name, date|
.
.
.
end
174
Within the two pipes (or bars), we assign the key and value to two variables.
The block is like an unnamed method. The two variables are available only
within the block. As each key-value pair is presented by the iterator, the variables are assigned, and the statements in the block are executed.
In our example code, we evaluate each date in the famous_birthdays hash
to determine if the musician was born in December. When we find a December
birthday, we add the name of the musician to the born_in_december array:
famous_birthdays.each do |name, date|
if date.month == 12
born_in_december << name
end
end
When you use a block within a method, any variable in your method is available
within the block. Thats why we can add name to the array born_in_december.
Computer scientists consider a block to be a programming language construct
called a closure. Ruby has other closures, including the proc (short for procedure) and the lambda. Though blocks are common youll seldom see procs or
lambdas in ordinary Rails code. They are more common in the Rails source
code where advanced programming techniques are used more frequently.
The key point to know about a block (or a proc or a lambda) is that it works
like a method. Though you dont see a method definition, you can use a block
to evaluate a sequence of statements and obtain a result.
MORE RUBY
175
Nothing in the exercise is Rails. The example code only uses keywords from
the Ruby API.
Rails has its own API, with hundreds of classes and methods. The Rails API
uses the syntax and keywords of the Ruby language to construct new classes
and create new keywords that are specific to Rails and useful for building web
applications.
We say Ruby is a general-purpose language because it can be used for anything. Rails is a domain-specific language (DSL) because it is used only by
people building web applications (in this sense, domain means area or field
of activity). Ruby is a great language to use for building a DSL, which is why
it was used for Rails. Unlike some other programming languages, Ruby easily can be extended or tweaked. For example, developers can redefine classes,
add extra methods to existing classes, and use the special method_missing
method to handle method calls that arent previously defined. Software architects call this metaprogramming which simply means clever programming that
twists and reworks the programming language.
When you add a gem to a Rails project, youll add additional keywords. Some
of the most powerful gems add their own DSLs to your project. For example,
the Cucumber gem provides a DSL for turning user stories into automated tests.
Adding Rails, additional gems, and DSLs provides powerful functionality at
the cost of complexity. But it all conforms to the syntax of the Ruby language.
As you learn to recognize Ruby keywords and language structures, youll be
able to pick apart the complexity and make sense of any code.
More Ruby
To develop your proficiency as a Rails developer, I hope you will make an
effort to learn Ruby as you learn Rails. Dont be lazy; when you encounter a
bit of Ruby you dont understand, make an effort to find out what is going on.
Spend time with a Ruby textbook or interactive course when you work on Rails
projects.
176
Collaborative Learning
The best way to learn Ruby is to actually use it. Thats the concept behind this
site:
Exercism.io
With Exercism, youll work though code exercises and get feedback from other
learners.
Online Tutorials
TryRuby.org - free browser-based interactive tutorial from Code School
Codecademy Ruby Track - free browser-based interactive tutorials from
Codecademy
Ruby Monk - free browser-based interactive tutorial from C42 Engineering
Ruby Koans - free browser-based interactive exercises from Jim Weirich
and Joe OBrien
Ruby in 100 Minutes - free tutorial from JumpstartLab
Code Like This - free tutorials by Alex Chaffee
RailsBridge Ruby - basic introduction to Ruby
CodeSchool Ruby Track - instructional videos with in-browser coding
exercises
GIT
177
Books
Learn To Program - free ebook by Chris Pine
Learn To Program - expanded $18.50 ebook by Chris Pine
Learn Code the Hard Way - free from Zed Shaw and Rob Sobers
Beginning Ruby - by Peter Cooper
Programming Ruby - by Dave Thomas, Andy Hunt, and Chad Fowler
Eloquent Ruby - by Russ Olsen
Books by Avdi Grimm, including Confident Ruby and Objects on Rails.
Newsletters
Practicing Ruby - $8/month for access to over 90 helpful articles on Ruby
RubySteps - weekly lessons by email from Pat Maddox
Screencasts
RubyTapas - $9/month for access to over 100 screencasts on Ruby
Git
Theres no need to save the file lib/example.rb file we created to learn Ruby.
You can simply delete the file:
178
$ rm lib/example.rb
Chapter 16
Template Languages
HTML is intended for markup, which means applying formatting to a text file.
For a web application, ordinary HTML is not sufficient; we need to mix in
Ruby code. Well use a templating language that gives us a syntax for mixing
HTML tags and Ruby code. The Ruby code will be processed by a templating
engine built into Rails. The output will be pure HTML sent to the browser.
The most popular templating language available for Rails is ERB, Embedded
Ruby, which is the Rails default.
179
180
In the Concepts chapter in Book One, you learned that components of Rails
can be mixed for different stacks. Some developers substitute Haml or Slim
for ERB. Well use ERB in this book because it is the most popular.
The first line in the file contains an HTML heading tag, <h3>, with headline
text, Home.
When you used the browser diagnostic view to see the HTML file received by
the server, you saw this:
<!DOCTYPE html>
<html>
<head>
<title>LearnRails</title>
<link data-turbolinks-track="true"
href="/assets/application.css?body=1" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/application.js?body=1"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="NRPrgfuj5GAyylNpNxQaMHDypcOsu6dmh5DT1yET6hQ=" name="csrf-token" />
</head>
<body>
<h3>Home</h3>
<p>Welcome to the home of Foobar Kadigan.</p>
<p>I was born on 1990-09-22.</p>
<p>Only 126 days until my birthday!</p>
181
</body>
</html>
If youve built websites before, youll recognize the HTML file conforms to
the HTML5 specification, with a DOCTYPE, <head> and <body> tags, and
miscellaneous tags in the HEAD section, including a title and various CSS and
JavaScript assets.
If you look closely, youll see some HTML attributes you might not recognize,
for example the data-turbolinks-track attribute. That is added by Rails
to support turbolinks, for faster loading of webpages.
For the most part, everything is ordinary HTML. But only part of it originates
from the view file weve created for our home page.
182
<!DOCTYPE html>
<html>
<head>
<title>LearnRails</title>
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
Static pages delivered from the public folder do not use the default application
layout. But every page generated by the model-view-controller architecture in
the app/ folder incorporates the default application layout, unless you specify
otherwise.
The default application layout is where you put HTML that you want to include
on every page of your website.
Remember when we looked at the hidden code in the controller that renders a
view? The controller uses the render method to combine the view file with
the application layout.
Heres the Visitors controller, again, with the hidden render method revealed:
class VisitorsController < ApplicationController
def new
@owner = Owner.new
render 'visitors/new'
end
end
YIELD
183
Alternatively, you could tell the controller to render the view without any application layout:
render 'visitors/new', :layout => false
Yield
How does the render method insert the view file in the application layout?
Notice that the default application layout contains the Ruby keyword yield.
.
.
.
<%= yield %>
.
.
.
The yield keyword is replaced with a view file that is specific to the controller
and action, in this case, the app/views/visitors/new.html.erb view file.
The content from the view is inserted where you place the yield keyword.
184
Yield Variations
We wont do it, but you could also use the yield keyword to insert a sidebar
or a footer.
Rails provides ways to insert content into a layout file at different places. The
content_for method is helpful when your layout contains distinct regions
such as sidebars and footers that should contain their own blocks of content.
For example, you could create an application layout that includes a sidebar.
This is just an example, so dont add it to the application you are building:
<!DOCTYPE html>
<html>
<head>
<title>LearnRails</title>
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="main">
<%= yield %>
</div>
<div class="sidebar">
<%= yield :sidebar %>
</div>
</body>
</html>
This view file provides both the main content and a sidebar:
<% content_for :sidebar do %>
<h3>Contact Info</h3>
<p>Email: [email protected]</p>
<% end %>
<h3>Main</h3>
<p>Welcome!</p>
This section gets inserted at the <%= yield :sidebar %> location:
ERB DELIMITERS
185
The rest of the file gets inserted at the main <%= yield %> location.
Again, dont add this to your application. Im just offering it as an example of
multiple yield statements.
The reference RailsGuides: Layouts and Rendering in Rails explains more
about using yield and content_for.
ERB Delimiters
Earlier, we saw ERB <%= ... %> delimiters allow us to insert Ruby expressions which are replaced by the result of evaluating the code. Here is an
example that displays the number 4
<%= 2 + 2 %>
Look closely and youll see this ERB delimiter is slightly different:
<% 3.times do %>
<li>list item</li>
<% end %>
An ERB delimiter that does not contain the = (equals) sign will execute Ruby
code but will not display the result. It is commonly used to add Ruby blocks to
HTML code, so youll often see do and end statements within ERB <% ...
%> delimiters. The example above will create three list items, like this:
186
<li>list item</li>
<li>list item</li>
<li>list item</li>
It is only used for adding comments. The expression within the ERB <%# ...
%> delimiters will not execute and will not appear when the page is output as
HTML.
187
Why do we need this cryptic code? It turns out that almost any website that
accepts user input via a form is vulnerable to a security bug (an exploit) named
a cross-site request forgery. To prevent rampant CSRF exploits, the Rails core
team includes the csrf_meta_tags view helper in the default application
layout. Rails provides a number of similar features that make websites more
secure.
A Rails view file becomes much less mysterious when you realize that many of
the keywords you see are view helpers. Strange new keywords may be part of
the Rails API. Or they may be provided by gems youve added (gem developers
often use the Ruby DSL capability to create new keywords). Think of it this
way: Ruby gives developers the power to create an unlimited number of new
HTML tags. These tags are not really HTML because they are not part of the
HTML specification. But they serve as shortcuts to produce complex snippets
of HTML and content.
Now that weve learned about view helpers, we can start building our default
application layout.
188
The -force argument will force the gem to replace the existing app/views/layouts/applicat
file.
If you have the app/views/layouts/application.html.erb file open in your text
editor, it will change.
BASIC BOILERPLATE
189
Basic Boilerplate
Open the file app/views/layouts/application.html.erb:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Learn Rails" %></title>
<meta name="description" content=
190
Adding Boilerplate
Webmasters who build static websites are accustomed to setting up web pages
with boilerplate, or basic templates for a standard web page. The well-known
HTML5 Boilerplate project has been recommending best practice tweaks to
web pages since 2010. Very few of the HTML5 Boilerplate recommendations
are relevant for Rails developers, as Rails already provides almost everything
ADDING BOILERPLATE
191
required. Well discuss one important boilerplate item and a few nice to have
extras.
If you want to learn more, the article HTML5 Boilerplate for Rails Developers
looks at the recommendations.
Viewport
The viewport metatag improves the presentation of web pages on mobile
devices. Setting a viewport tells the browser how content should fit on the
devices screen. The tag is required for either Bootstrap or Zurb Foundation
front-end frameworks.
The viewport metatag looks like this:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
192
The rails_layout gem has created a default title and description based on our
project name.
Later in the tutorial, well see how to use a content_for statement to set a
title and description for each individual page.
The code is complex if you havent seen advanced Ruby before. It uses the
Ruby ternary operator which maximizes compactness at the price of introducing obscurity. Youll recall from the Just Enough Ruby chapter that it
is a fancy conditional statement that says, if content_for?(:title) is
present in the view file, use yield(:title) to include it, otherwise just display Learn Rails.
Asset Pipeline
You may have noticed these Rails helper methods:
stylesheet_link_tag
javascript_include_tag
These are tags that add CSS and JavaScript to the web page using the Rails
asset pipeline.
The Rails asset pipeline utility is one of the most powerful features of the platform. It offers convenience to the developer and helps organize an application; more importantly, it improves the speed and responsiveness of any complex website. If youre going to do any front-end development with CSS or
JavaScript in Rails, you must understand the Rails asset pipeline. Heres how
it works.
ASSET PIPELINE
193
The same is true for CSS files in non-Rails websites. You add a <link> tag
for each stylesheet file. With multiple stylesheets, the HEAD section of your
application layout might look like this:
<!DOCTYPE html>
<html>
<head>
<title>Page that uses multiple CSS files</title>
<link href="core.css" rel="stylesheet" type="text/css" />
<link href="site.css" rel="stylesheet" type="text/css" />
<link href="custom.css" rel="stylesheet" type="text/css" />
</head>
If you want to handle CSS and JavaScript without Rails, you can place your
files in the public folder. If you do so, every time you add a JavaScript or CSS
file, you must modify the application layout file. Instead, use the asset pipeline
and simplify this.
194
app/assets/javascripts/
app/assets/stylesheets/
Any JavaScript and CSS file you add to these folders is automatically added to
every page.
In development, when the web browser makes a page request, the files in the
asset pipeline folders are combined together and concatenated as single large
files, one for JavaScript and one for CSS.
If you examine the application layout file, youll see the tags that perform this
service:
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
Using the asset pipeline, there is no need to modify the application layout file
each time you create a new JavaScript or CSS file. Create as many files as you
need to organize your JavaScript or CSS code and, in production, youll automatically get one single file delivered to the browser. In development mode,
Rails continues to deliver multiple files for easier debugging.
In production, theres a big performance advantage with the asset pipeline. Requesting files from the server is a time-consuming operation for a web browser,
so every extra file request slows down the browser. The Rails asset pipeline
eliminates the performance penalty of multiple <script> or <link> tags.
The Rails asset pipeline also compresses JavaScript and CSS files for faster
page loads.
NAVIGATION LINKS
195
Navigation Links
Every website needs navigation links.
You can add navigation links directly to your application layout but many Rails
developers prefer to create a partial template -a partial-to better organize
the default application layout.
Introducing Partials
A partial is similar to any view file, except the filename begins with an underscore character. Place the file in any view folder and you can use the render
keyword to insert the partial.
Were not going to add a footer to our tutorial application, but here is how we
could do it. Wed use the render keyword with a file named app/views/layouts/_footer.htm
<%= render 'layouts/footer' %>
Notice that you specify the folder within the app/views/ directory with a truncated version of the filename. The render method doesnt want the _ underscore character or the .html.erb file extension. That can be confusing; it
makes sense when you remember that Rails likes convention over configuration and economizes on extra characters when possible.
196
Were not going to add a footer to our application, but we will add navigation
links by using a partial. First, lets learn about link helpers.
Rails gives us another option, however. We can use the Rails link_to view
helper instead of the HTML <a> anchor tag. The Rails link_to helper eliminates the crufty <> angle brackets and the unnecessary href="". More importantly, it adds a layer of abstraction, using the routing configuration file to
form links. This is advantageous if we make changes to the location of the
link destinations. Earlier, when we created a static About page, we first
set the config/routes.rb file with a route to the About page: root to:
redirect(/about.html). Later we removed the static About page and
set the config/routes.rb file with a route to the dynamic home page: root to:
visitors#new. If we used the raw HTML <a> anchor tag, wed have to
change the raw HTML everywhere we had a link to the home page. Using the
Rails link_to helper, we name a route and make any changes once, in the
config/routes.rb file.
When you use the Rails link_to helper, youll avoid the problem of link maintenance that webmasters face on static websites. Some webmasters like to use
absolute URLs, specifying a host name in the link, for example http://www.example.co
Absolute URLs are a headache when moving the site, for example from staging.example
to www.example.com. The problem is avoided by using relative URLs, such
as /about.html, about.html, or even ../about.html. But relative URLs
NAVIGATION LINKS
197
are fragile, and moving files or directories often results in overlooked and broken links. Instead, with the Rails link_to helper, you always get the destination location specified in the config/routes.rb file.
Navigation Partial
Examine the app/views/layouts/application.html.erb and youll see the use
of the navigation partial.
We include the navigation partial in our application layout with the expression:
.
.
.
<%= render 'layouts/navigation' %>
.
.
.
198
.
.
.
<%= render 'layouts/navigation_links' %>
.
.
.
This demonstrates that one partial can include another partial, so that partials
can be nested.
Flash Messages
Rails provides a standard convention to display alerts (including error messages) and other notices (including success messages), called a flash message.
The name comes from the term flash memory and should not be confused
with the Adobe Flash web development platform that was once popular for
FLASH MESSAGES
199
animated websites. The flash message is documented in the RailsGuides: Action Controller Overview.
Heres a flash message you might see after logging in to an application:
If you test the application after adding the messages to the VisitorsController,
youll see two flash messages appear on the page.
200
Rails provides the flash object so that messages can be created in the controller and displayed on the rendered web page.
In this example, we create a flash message by associating the object flash[:notice]
with the string Welcome!. We can assign other messages, such as flash[:alert]
or even flash[:warning]. In practice, Rails uses only :notice and :alert
as flash message keys so it is wise to stick with just these.
In our example above, we really need to use the flash.now variant because the
controller provides a hidden render method. Update the file app/controllers/visitors_cont
class VisitorsController < ApplicationController
def new
@owner = Owner.new
flash.now[:notice] = 'Welcome!'
flash.now[:alert] = 'My birthday is soon.'
end
end
Using flash.now will make sure the message only appears on the rendered
FLASH MESSAGES
201
page and will not persist after a user follows a link to a new page.
If you ever see a sticky flash message that wont go away, you need to use
flash.now instead of flash.
In this simple example, we use each to iterate through the flash hash, retrieving
202
a key and value that are passed to a block to be output as a string. Weve
chosen the variable names key and value but the names are arbitrary. In the
next example, well use name and msg as variables for the key-value pair. The
output string will appear as HTML like this:
<div class="notice">Welcome!</div>
<div class="alert">My birthday is soon.</div>
It improves on our simple Ruby example in several ways. First, the expression
if msg.is_a?(String) serves as a test to make sure we only display messages that are strings. Second, we use the Rails content_tag view helper to
create the HTML div. The content_tag helper eliminates the messy soup
of angle brackets and quote marks we used to create the HTML output in the
example above. Finally, we apply a CSS class and combine the word flash
with notice or alert to make the CSS class.
We include the flash messages partial in our application layout with the expression:
HTML5 ELEMENTS
203
.
.
.
<%= render 'layouts/messages' %>
.
.
.
HTML5 Elements
Lets look again at the app/views/layouts/application.html.erb file.
To complete our examination of the application layout file, well look at a few
structural elements. These elements are not unique to a Rails application and
will be familiar to anyone who has done front-end development.
Notice the tags that are structural elements in the HTML5 specification:
<header>
<main>
These elements add structure to a web page. The tags dont add any new behavior but make it easier to determine the structure of the page and apply CSS
styles.
We wrap the navigation partial in the <header> tag:
<header>
<%= render 'layouts/navigation' %>
</header>
204
<main role="main">
<%= render 'layouts/messages' %>
<%= yield %>
</main>
Application Layout
Our application layout is complete. We dont have to add anything because the
rails_layout gem has created everything we need.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
SIMPLE CSS
205
Simple CSS
So far, weve examined four files that were added by the rails_layout gem:
app/views/layouts/application.html.erb
app/views/layouts/_messages.html.erb
app/views/layouts/_navigation.html.erb
app/views/layouts/_navigation_links.html.erb
206
Lets examine the CSS file that was created by the rails_layout gem.
Open the file app/assets/stylesheets/simple.css:
/*
* Simple CSS stylesheet for a navigation bar and flash messages.
*/
main {
background-color: #eee;
padding-bottom: 80px;
width: 100%;
}
header {
border: 1px solid #d4d4d4;
background-image: linear-gradient(to bottom, white, #f2f2f2);
background-color: #f9f9f9;
-webkit-box-shadow: 0 1px 10px rgba(0, 0, 0, 0.1);
-moz-box-shadow: 0 1px 10px rgba(0, 0, 0, 0.1);
box-shadow: 0 1px 10px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
ul.nav li {
display: inline;
}
ul.nav li a {
padding: 10px 15px 10px;
color: #777777;
text-decoration: none;
text-shadow: 0 1px 0 white;
}
.flash_notice, .flash_alert {
padding: 8px 35px 8px 14px;
margin-bottom: 20px;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
border: 1px solid #fbeed5;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 20px;
}
.flash_notice {
background-color: #dff0d8;
border-color: #d6e9c6;
color: #468847;
}
.flash_alert {
background-color: #f2dede;
border-color: #eed3d7;
207
color: #b94a48;
}
If you already know CSS, youll see weve set a background color for the main
section of the page. Weve also set styles for a header, navigation links, and
flash messages. This book is about Rails, not CSS, so we wont examine this
closely. For more on CSS, there are thousands of tutorials on the web, but I
like these:
Codeacademy
HTML Dog
Remember what we learned about the Rails asset pipeline. By default, any
CSS file in the app/assets/stylesheets/ folder will be added automatically to
the application.css file that is included in the default application layout.
In the next chapter, we remove the app/assets/stylesheets/simple.css and use
Zurb Foundation to supply styles for the header, navigation links, and flash
messages. For now, the simple.css file adds some basic styling and layout to
the application.
208
Git
Lets commit our changes to the Git repository and push to GitHub:
$ git add -A
$ git commit -m "update application layout"
$ git push
Chapter 17
Front-End Framework
This chapter discusses front-end development and design using CSS. Ill show
you how to add style to a Rails application, using Zurb Foundation for a simple
theme.
What do we mean by front-end development? A website back end is the Rails
application that assembles files that are sent to the browser, plus a database and
any other server-side services. A website front end is all the code that runs
in the browser. Everything that controls the appearance of the website in the
browser is the responsibility of a front-end developer, including page layout,
CSS stylesheets, and JavaScript code.
Front-end development has grown increasingly important as websites have become more sophisticated. And front-end technology has grown increasingly
complex, to the degree that front-end development has become a job for specialists.
Front-end developers are primarily concerned with:
markup - the layout and structure of the page
style - graphic design for visual communication
interactivity - browser-based visual effects and user interaction
209
210
Broader concerns include:
CSS Frameworks
Web developers began putting together boilerplate CSS stylesheets as early
as 2000, when browsers first began to fully support CSS. Boilerplate CSS made
it easy to reuse CSS stylesheet rules from project to project. More importantly,
designers often implemented CSS reset stylesheets to enforce typographic
uniformity across different browsers.
Engineers at Yahoo! released the Yahoo! User Interface Library (YUI) as an
open source project in February 2006. Inspired by an article by Jeff Croft,
and reacting to the huge size of the YUI library, independent developers began
releasing other CSS frameworks such as the 960 grid system and the Blueprint
CSS framework.
There are dozens of CSS frameworks. In general, they all seek to implement a
common set of requirements:
An easily customizable grid
Some default typography
211
A typographic baseline
CSS reset for default browser styles
A stylesheet for printing
More recently, with the ubiquity of smartphones and tablets, CSS frameworks
support responsive web design, accommodating differences in screen sizes
across a range of devices.
In tandem with the development of CSS frameworks, weve seen the emergence
of JavaScript libraries and frameworks.
212
Front-End Frameworks
Front-end frameworks combine CSS and JavaScript libraries. Many elements
that are found on sophisticated web pages, such as modal windows or tabs,
213
214
dation rely on CSS preprocessors; LESS for Bootstrap 3 and Sass for Bootstrap
4 and Zurb Foundation. LESS and Sass extend CSS to give it more powerful
programming language features. As a result, your stylesheets can use variables,
mixins, and nesting of CSS rules, just like a real programming language.
For example, in Sass you can create a variable such as $blue: #3bbfce and
specify colors anywhere using the variable, such as border-color: $blue.
Mixins are like variables that let you use snippets of reusable CSS. Nesting
eliminates repetition by layering CSS selectors.
Sass is generally recognized as more powerful than LESS, and Sass is included
in any new Rails application.
215
Before I show you how to integrate Zurb Foundation with your Rails application, lets briefly consider matters of design.
216
217
With the -force argument, the rails_layout gem will replace existing files.
The gem will create the file:
app/assets/stylesheets/1st_load_framework.css.scss
and modify the files:
app/assets/javascripts/application.js
app/views/layouts/_messages.html.erb
app/views/layouts/_navigation.html.erb
app/views/layouts/application.html.erb
It will also remove the file:
app/assets/stylesheets/simple.css
Finally, it will modify the file config/application.rb.
Lets examine the files to see how our application is configured to use Zurb
Foundation.
218
Before you continue, make sure that the rails_layout gem renamed the app/assets/stylesheet
file as app/assets/stylesheets/application.css.scss. Otherwise you wont see
the CSS styling we will apply.
app/assets/stylesheets/
219
The asset pipeline helps web pages display faster in the browser by combining
all CSS files into a single file (it does the same for JavaScript).
Lets examine the file app/assets/stylesheets/application.css.scss:
/*
* This is a manifest file that'll be compiled into application.css, which will include all the
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheet
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of t
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
*= require_tree .
*= require_self
*/
220
A Manifest File
Its not obvious from the name of the app/assets/stylesheets/application.css.scss
file that it serves as a manifest file as well as a location for miscellaneous CSS
rules. For most websites, you can ignore its role as a manifest file. In the comments at the top of the file, the *= require_self directive indicates that any
CSS in the file should be delivered to the browser. The *= require_tree .
directive (note the Unix dot operator) indicates any files in the same folder,
including files in subfolders, should be combined into a single file for delivery
to the browser.
If your website is large and complex, you can remove the *= require_tree
. directive and specify individual files to be included in the file that is generated by the asset pipeline. This gives you the option of reducing the size of the
application-wide CSS file that is delivered to the browser. For example, you
might segregate a file that includes CSS that is used only in the sites administrative section. In general, only large and complex sites need this optimization.
The speed of rendering a single large CSS file is faster than fetching multiple
files.
221
Note that this configuration is different from the instructions provided in the
Foundation Documentation. In keeping with Rails best practices, we load the
Foundation JavaScript libraries using the asset pipeline in the <head> section
of the default application layout. Using the jQuery DOM ready event to load
Foundation insures that Foundation is compatible with other jQuery plugins or
JavaScript code.
222
223
Take a look at the Foundation documentation to see your options. Here are just
a few examples:
buttons
pricing tables
modal dialogs
At a simpler level, Foundation provides a collection of carefully-crafted styling
rules in the form of CSS classes. These are building blocks you use for page
layout and typographic styling. For example, Foundation gives you CSS classes
to set up rows and columns in a grid system.
Lets take a closer look at the Foundation grid system.
Foundation Grid
The Foundation grid is responsive because it has breakpoints. There are actually three grids:
Small: browser windows 0 to 640 pixels wide (phones)
Medium: browser windows 641 to 1023 pixels wide (tablets)
Large: browser windows 1024 pixels and wider (desktops)
Start by designing for the small screen with the classes prefixed small; then
add classes prefixed medium or large if you want a layout for larger screens.
The layout will change at each breakpoint.
The grid gives you 12 columns by default. You can organize your layout in
horizontal and vertical sections using row and columns classes.
For example, you could use Foundation grid classes to set up an application
layout with a footer as a row with two sections:
224
<footer class="row">
<section class="small-4 columns">
Copyright 2014
</section>
<section class="small-8 columns">
All rights reserved.
</section>
</footer>
The Foundation row class will create a horizontal break. The footer will contain two side-by-side sections. The first will be four columns wide; the second
will be eight columns wide.
Heres the same footer with a responsive design:
<footer class="row">
<section class="small-12 medium-4 columns">
Copyright 2014
</section>
<section class="small-12 medium-8 columns">
All rights reserved.
</section>
</footer>
On desktops and tablets, the footer will contain two side-by-side sections. On
phones, each section will expand to take the full browser width, appearing as
stacked rows.
To better understand the grid system with all its options, see the documentation
for the Foundation Grid.
225
Other developers take issue with this approach. They argue that Foundations
markup is often presentational, with class names describing the appearance of
the page. In an ideal world, all markup would be semantic, with class names
describing the function or purpose of a style. For example, a submit button
often needs styling. Compare these two approaches to markup:
presentational: <button class="big red button">Order Now</button>
semantic: <button class="submit">Order Now</button>
Suppose your user testing indicates a green button generates more sales. With
the presentational approach youd have to change both the Rails view file and
the CSS file. With a semantic approach, youd just change the CSS file to
reassign the color of the submit class.
226
Sass mixins add a layer of complexity that can map Foundation class names to
your own semantic class names.
For example, the Foundation grid system is presentational. Specifying rows
and columns, and quantifying the size of columns, describes the visual appearance of sections of the layout rather than the purpose of each section. The presentational approach makes it easy to visualize the layout of a page. But youll
be tied to Foundation 5.0 class names for the life of your website. If class
names change in Foundation 6.0, or you decide to switch to another front-end
framework, it will be difficult to update your application, as you will have to
carefully rebuild each view file.
Is it worth the effort to add the complexity of Sass mixins just to future-proof
your website? Probably not for a simple website such as the one you are building for Foobar Kadigan.
The rails_layout gem uses Sass mixins to apply CSS style rules to the default application layout. In doing so, the default application layout is free of
framework-specific code and can be used with either Bootstrap or Zurb Foundation.
Before we examine the default application layout, lets take a look at the Sass
mixins supplied by the rails_layout gem.
Look again at the file app/assets/stylesheets/1st_load_framework.css.scss created by the rails_layout gem:
// import the CSS framework
@import "foundation";
// override for the 'Home' navigation link
.top-bar .name {
font-size: rem-calc(13);
line-height: 45px; }
.top-bar .name a {
font-weight: normal;
color: white;
padding: 0 15px; }
// THESE ARE EXAMPLES YOU CAN MODIFY
// create mixins using Foundation classes
@mixin twelve-columns {
227
@extend .small-12;
@extend .columns;
}
@mixin six-columns-centered {
@extend .small-6;
@extend .columns;
@extend .text-center;
}
// create your own classes
// to make views framework-neutral
.column {
@include six-columns-centered;
}
.form {
@include grid-column(6);
}
.form-centered {
@include six-columns-centered;
}
.submit {
@extend .button;
@extend .radius;
}
// apply styles to HTML elements
// to make views framework-neutral
main {
@include twelve-columns;
background-color: #eee;
}
section {
@extend .row;
margin-top: 20px;
}
The rails_layout gem is in active development so the file youve created may
be different from the example in this tutorial. It will probably be very similar.
At the top of the file we import the Foundation framework CSS files from the
gem.
We override two Foundation style rules so the Home navigation link matches
the other links in the navigation bar.
Then we use mixins to create semantic classes.
Mixins are declared in Sass files by the @mixin directive, which takes a block
of CSS styles, other mixins, or a CSS selector (a CSS class or ID).
228
If youd like to combine CSS classes, or rename a CSS class, use the @extend
directive to add a CSS class to a mixin.
The first declaration @mixin twelve-columns combines the Foundation classes
small-12 and columns to make a new class, twelve-columns.
The second declaration @mixin six-columns-centered makes a column
that is six columns wide with centered text.
Next we create a few classes that use the mixins or combine Foundation CSS
classes. For example, the new submit class can be used for a rounded button.
When we use it in a view, this class will be purely semantic since it describes
the purpose of the element, allowing us to set its appearance outside of any
view file.
Finally, to avoid applying Foundation classes in the application layout file,
we apply styles to HTML elements main and section to make the views
framework-neutral. We use the @include directive to add the mixins we need.
We also use the @extend directive to add a Foundation CSS class. And we directly set CSS properties such as background-color and margin-top.
Using this technique, the file app/assets/stylesheets/1st_load_framework.css.scss
becomes the single point of intersection between the Foundation framework
and the application layout. For a simple website, this could be over-engineering
and counter-productive. The rails_layout gem uses the technique so that either
Bootstrap or Zurb Foundation can be used without any change to the default
application layout.
Well use the CSS classes provided by the rails_layout gem in the tutorial application, but if you choose to customize the application, feel free to use Foundation classes directly to keep your project simple.
229
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Learn Rails" %></title>
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Le
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%# Modernizr is required for Zurb Foundation %>
<%= javascript_include_tag 'vendor/modernizr' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<header>
<%= render 'layouts/navigation' %>
</header>
<main role="main">
<%= render 'layouts/messages' %>
<%= yield %>
</main>
</body>
</html>
This file is almost identical to the simple application layout file we looked at in
the previous chapter.
Because weve applied Foundation classes to the HTML element main in the
app/assets/stylesheets/1st_load_framework.css.scss file, theres no need to
use Foundation classes directly in the application layout.
230
.
.
config/application.rb
Just for a minute, were going to dive deeply into a complicated issue that arises
when integrating Foundation with Rails. You could skip this section, because
the rails_layout gem takes care of the messy details. But if you read it, you will
understand a little more about the mysteries of the Rails asset pipeline.
Earlier I briefly noted that the rails_layout gem makes a necessary change to
the config/application.rb file. It adds:
module LearnRails
class Application < Rails::Application
.
.
.
# For Foundation 5
config.assets.precompile += %w( vendor/modernizr )
end
end
231
Sprockets::Rails::Helper::AssetFilteredError at /
Asset filtered out and will not be served:
add `config.assets.precompile += %w( vendor/modernizr )`
to `config/application.rb` and restart your server
This is a sanity check to reveal asset pipeline errors that only show up in production (details).
For now, you can forget you learned this. But now youll understand the issue
if you ever see this error.
You can avoid this issue by never adding an extra javascript_include_tag
to a layout and always adding JavaScript files to the assets/ folder where they
are compiled automatically.
We use each to iterate through the flash hash, retrieving a name and msg that
are passed to a block to be output as a string. The expression if msg.is_a?(String)
serves as a test to make sure we only display messages that are strings. We
construct a div that applies Foundation CSS styling around the message. Foundation recognizes a class alert-box and round (for rounded corners). A
232
class of either success or alert styles the message. Rails notice messages
will get styled with the Foundation success class. Any other Rails messages,
including alert messages, will get styled with the Foundation alert class.
We use the Rails content_tag view helper to create a div containing the
message.
Finally, we create a close icon by applying the class close to a link. We
use the HTML entity × (a big X character) for the link; it could be
the word close or anything else we like. Foundations integrated JavaScript
library will hide the alert box when the close link is clicked.
Foundation provides detailed documentation if you want to change the styling
of the alert boxes.
The navigation partial is now more complex, with layout and Foundation classes
needed to produce a responsive navigation bar.
233
At the conclusion of this chapter, youll test the responsive navigation by resizing the window. At small sizes, the navigation links will disappear and be
replaced by an icon labeled Menu. Clicking the icon will reveal a vertical
menu of navigation links. The navigation menu is a great demonstration of
the ability of Zurb Foundation to adjust to the small screen size of a tablet or
smartphone.
If youd like to add a site name or logo to the tutorial application, you can
replace the link helper <%= link_to Home, root_path %>. It is important to preserve the enclosing layout and classes, even if you dont want
to display a site name or logo. The enclosing layout is used to generate the
navigation menu when the browser window shrinks to accommodate a tablet or
smartphone.
Youll see we wrap the nested partial render layouts/navigation_links
with a Foundation class to complete the navigation bar.
234
Here the SimpleForm gem uses the rails generate command to create files
for initialization and localization (language translation). SimpleForm can be
customized with settings in the initialization file. Well use the defaults.
235
236
Git
Lets commit our changes to the Git repository and push to GitHub:
$ git add -A
$ git commit -m "front-end framework"
$ git push
Chapter 18
Add Pages
Lets begin adding pages to our web application.
There are three types of web pages in a Rails application. Weve looked at two
types so far:
static pages in the public/ folder that contain no Ruby code
dynamic pages such as our home page that use the application layout
Theres another type of web page that is required on many websites. It has
static content; that is, no dynamic data is needed on the page. But it uses the
default application layout to maintain consistency in the website look and feel.
We classify this type of page as a:
static view that uses the application layout
Examples include:
About page
Legal page
237
238
FAQ page
Its possible to place these pages in the public/ folder and copy the HTML and
CSS from the default application layout but this leads to duplicated code and
maintenance headaches. And dynamic elements such as navigation links cant
be included. For these reasons, developers seldom create static pages in the
public/ folder.
Alternatively, a dynamic page can be created that has no model, a nearly-empty
controller, and a view that contains no instance variables. This solution is quite
common for static views that use the application layout.
This solution is implemented so frequently that many developers create a gem
to encapsulate the functionality. Were going to use the best-known of these
gems, the high_voltage gem created by the Thoughtbot consulting firm.
Well use the High Voltage gem to create an About Page.
We also will create a Contact page. Well again use the High Voltage gem,
but only for the first version of the Contact page. Later well discard the page
we created with the High Voltage gem and replace it with a full model-viewcontroller implementation. The process will show the difference between an
older form of web application architecture and a newer Rails way.
VIEWS FOLDER
239
gem 'high_voltage'
Views Folder
Create a folder app/views/pages:
$ mkdir app/views/pages
Any view files we add to this directory will automatically use the default application layout and appear when we use a URL that contains the filename.
The High Voltage gem contains all the controller and routing magic required
for this to happen.
Lets try it out.
About Page
Create a file app/views/pages/about.html.erb:
<% content_for :title do %>About<% end %>
<h3>About Foobar Kadigan</h3>
<p>He was born in Waikikamukau, New Zealand. He left New Zealand for England,
excelled at the University of Mopery, and served in the Royal Loamshire Regiment.
While in service, he invented the kanuten valve used in the processing of
unobtainium for industrial use. With a partner, Granda Fairbook, he founded
Acme Manufacturing, later acquired by the Advent Corporation, to develop his
discovery for use in the Turboencabulator. Mr. Kadigan is now retired and
lives in Middlehampton with a favorite cat, where he raises Griadium frieda
and collects metasyntactic variables.</p>
<p>His favorite quotation is:</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.</p>
240
Our simple About view will be combined with the default application layout
by the High Voltage gem.
We include a content_for Rails view helper that passes a page title to the
application layout.
Contact Page
For the initial version of the Contact page, create a file app/views/pages/contact.html.erb:
241
242
Rails.application.routes.draw do
root to: 'visitors#new'
end
Watch what happens when you resize the page. At smaller sizes, the navigation
bar changes to display a menu icon. Clicking the menu icon reveals a dropdown menu of navigation links. Youre seeing the power of the Foundation
framework.
Heres a troubleshooting tip. If clicking the menu icon doesnt reveal a dropdown menu, the application may not be loading the Foundation JavaScript library. Make sure that the file app/assets/javascripts/application.js contains:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require foundation
//= require_tree .
$(function() {
$(document).foundation();
});
Git
Lets commit our changes to the Git repository and push to GitHub:
$ git add -A
$ git commit -m "add 'about' and 'contact' pages"
$ git push
Chapter 19
Contact Form
Forms are ubiquitous on the web, to the degree we seldom notice how often
they are used for data entry, whether were logging into a website or posting
a blog comment. To build any interactive website, youll need to understand
forms. Here well build a contact form for our tutorial application.
A contact form is common on many websites. If you think about it, contact
forms are often unnecessary; simply displaying an email address is sufficient,
more convenient, and easier to implement. But building a contact form is an
excellent way to learn how to handle user data input. Well pretend that our odd
client, Mr. Foobar Kadigan, insists that he needs a contact form on his website.
Were not backing the tutorial application with a database so we wont store the
contact data after the information is submitted. Instead, in a subsequent chapter
well learn how to send the contents of the form by email to the website owner.
244
well see the approach has limitations. Well discard our first approach and
rebuild the Contact page, discovering how the Rails way is more powerful.
You may wonder why Im going to show you two different ways to implement
the contact form.
First, it is worthwhile to see there is more than one way to implement a web
application. Maturity as a software developer means imagining different approaches and evaluating your options. With this exercise, youll contrast two
approaches and see how we make choices about software architecture.
More importantly, it is not always obvious why we do things in a Rails way.
It would be easy to simply walk you through the steps to build a contact form
without showing you alternative implementations (thats how most tutorials do
it). But youll gain a deeper understanding of Rails by building the contact form
in a less sophisticated fashion and then seeing the more elegant Rails approach.
User Story
Lets plan our work with a user story:
*Contact Page*
As a visitor to the website
I want to fill out a form with my name, email address, and some text
In order to send a message to the owner of the website
Our first step will be to create a route to a controller that will process the submitted form.
Routing
Were going to create a ContactsController to process the submitted form data.
Every form must have a destination URL that receives the form submission.
ROUTING
245
The route post contact, to: contacts#process_form will create a route helper that generates a URL and hands off the request to a controller.
You can run the rake routes command to see our routes in the console:
$ rake routes
Prefix Verb URI Pattern
contact POST /contact(.:format)
root GET /
page GET /pages/*id
Controller#Action
contacts#process_form
visitors#new
high_voltage/pages#show
The output of the rake routes command is somewhat cryptic but confirms
weve created the routes we need.
The first item in the rake routes output indicates we can add contact to
_path to get our route helper, contact_path:
contact_path - a route helper that can be used in a controller or view
The second item indicates the request will be handled with the HTTP POST
protocol:
POST - HTTP method to submit form data
The third item indicates the application will respond to the following URL:
http://localhost:3000/contact - URL generated by the route helper
246
The fourth item indicates a request to the URL will be handled by:
contacts - the name of the controller
process_form - a controller action
For details about the syntax of routing directives, refer to RailsGuides: Routing
from the Outside In.
The route wont work yet; we need to create a ContactsController. But first
well create the form.
247
248
a name field
an email field
a content field
a submit button
Controller
We need code to process the form data. The form data is sent to the server
as a POST request attached to a URL. As weve learned, in Rails we use controllers to respond to browser requests. For this implementation, well create a
ContactsController to process the submitted form data.
Create a file app/controllers/contacts_controller.rb:
CONTROLLER
249
Params Hash
Take a close look at these two lines:
Rails.logger.debug "DEBUG: params are #{params}"
flash[:notice] = "Received request from #{params[:contact][:name]}"
250
Rails goes a step further and nests the form hash inside another hash named
params.
As pure Ruby code, the params hash looks like this:
params = {controller: 'contacts',
action: 'process_form',
contact: {name: 'Daniel', email: '[email protected]', content: 'hi!'}
}
The params hash includes these elements (plus others we wont cover):
current controller
current action
form data (our contact hash)
You will see the contents of the params hash in the console log after you submit the form. Well look at the console log when we test the implementation.
Process_form Method
Now that we know about the params hash, take a look again at the process_form
method:
def process_form
Rails.logger.debug "DEBUG: params are #{params}"
flash[:notice] = "Received request from #{params[:contact][:name]}"
redirect_to root_path
end
251
We use a logger.debug method to reveal the form data in our console log by
revealing the contents of the params hash.
Then we extract the data posted to the name field of the form and construct
a flash message. A hash containing the data from the contact form is nested
inside the params hash. We can retrieve the value of the name field with the
expression params[:contact][:name]. We use double quotes and string
interpolation to form the message using the #{...} syntax that evaluates a
Ruby expression and combines it with a string.
Finally we use the redirect_to directive to render the home page.
We havent actually sent the contact data to anyone. Well add code for that
later, after we refactor the controller to be a better example of the Rails way.
Before we do that, lets test the current implementation. Weve already set up
routing for the new controller.
252
The console log is our most important tool for debugging. Lets analyze what
we see:
Started POST - shows the server is responding to an HTTP POST request
/contact - the path portion of the URL
for 127.0.0.1 - the IP address for localhost
at . . . - timestamp
Processing by ContactsController - the controller
process_form - the controller action (the method that handles the request)
as HTML - not XML or some other markup
Parameters: - the params hash containing all the submitted data
utf8= - a Rails workaround to set the language encoding in Internet
Explorer
authenticity token - prevents CSRF security exploits
contact - a hash containing the form data
commit - the Submit label from the button
253
254
We would need additional code to test for invalid email addresses (it will be
a complex regex, or regular expression). And we would need a nicer way
of showing the error to the visitor (right now, raising the exception displays
an error message that makes it appear the application is broken). If we were
implementing this on another web application platform, we might go further
down this path, googling for code examples, and implementing a lengthy but
bulletproof validation function.
Rails offers a better way.
255
$ rm app/views/pages/contact.html.erb
256
ActiveModel
Rails extracts and generalizes common code that every website requires. The
code that websites need for access to databases is abstracted into the Rails
MODEL
257
Model
When we build database-backed applications with Rails, we base our models
on a parent class named ActiveRecord. We are not using a database for our
tutorial application, so well mix in behavior from ActiveModel, which adds
validation and error handling to our model. Lets set up a model that doesnt
require a database.
Create a file app/models/contact.rb:
class Contact
include ActiveModel::Model
attr_accessor :name, :string
258
When you copy this, be careful to keep the long regex expression (with:
/.../i) on one line (no line breaks).
We give the model the name Contact.
We mix in behavior from the ActiveModel class using include ActiveModel::Model.
We create attributes (data fields) for the model by using the attr_accessor
keyword, specifying that each attribute is a string. The attributes match the
fields in the contact form.
ActiveModel gives us validation methods named validates_presence_of,
validates_format_of, and validates_length_of. We check that name,
email, and content exist (no blanks are allowed). We provide a complex
regex, or regular expression, to test if the email address is valid. Finally, we
declare that the message content cannot exceed 500 characters.
The model is elegant. We describe the fields we need and state our validation
requirements. ActiveModel does all the rest.
Next well add a new Contact page by creating a view in the app/views/contacts/
folder.
The new contact form will use our new model.
259
The form is the same as we used before, but were now providing only one
argument, the @contact instance variable, to the SimpleForm view helper.
Thats enough to generate the form name and destination URL.
We havent yet created a controller that assigns the Contact model to the @contact
instance variable. Well do that soon.
Earlier, I explained that SimpleForm configures itself if we provide a model that
inherits from ActiveRecord. SimpleForm gives the form a name that matches
the model name. And SimpleForm generates a destination URL based on the
model name.
SimpleForm uses the @contact instance variable to name the form, set a destination for the form data, and initialize each field in the form using attributes
from the Contact model. Setting the values for the form fields from the attributes in the model is called binding the form to the object and you can
read about it in the RailsGuides: Form Helpers article.
260
Weve added the error_notification method which provides all the error
handling. The method call is very simple but the results will be impressive.
Well need a controller and routing to complete our model-view-controller architecture. But first, well detour to learn about seven standard controller actions.
261
262
Controller
Replace the contents of the file app/controllers/contacts_controller.rb:
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(secure_params)
if @contact.valid?
# TODO send message
flash[:notice] = "Message sent from #{@contact.name}."
redirect_to root_path
else
render :new
end
end
private
def secure_params
params.require(:contact).permit(:name, :email, :content)
end
end
CONTROLLER
263
Weve dropped the old school process_form method and added the Rails
way new and create methods.
The controller new action will instantiate an empty Contact model, assign it to
the @contact instance variable, and render the app/views/contacts/new.html.erb
view. Weve already created the view file containing the form.
SimpleForm will set a destination URL that corresponds to the ContactsController#create action. The create method will instantiate a new Contact model
using the data from the form (we take steps to avoid security vulnerabilities
firstmore on that later).
The ActiveModel class provides a method valid? which we can call on the
Contact model. Our conditional statement if @contact.valid? checks
each of the validation requirements weve set in the model.
If all the Contact fields are valid, we can send a message (not yet implemented),
prepare a flash message, and redirect to the home page. Notice that we dont
need to dig into the params hash for the visitors name; it is now available as
@contact.name directly from the model.
If any validation fails, the controller create action will render the app/views/contacts/new.
view. This time, appropriate error messages are set and the form objects
error_notification method will highlight the invalid field and display a
matching prompt.
Youre looking at the tightly bound interaction of the Rails way model, view,
and controller.
The only element we are missing is routing. But first, lets look closer at the
steps we take to avoid security exploits.
Mass-Assignment Vulnerabilities
Rails protects us from a class of security exploits called mass-assignment vulnerabilities. Rails wont let us initialize a model with just any parameters
submitted on a form. Suppose we were creating a new user and one of the user
264
Private Methods
If you paid close attention to the code you added to the Contacts controller, you
may have noticed the keyword private above the secure_params method
definition. This is a bit of software architecture that limits access to the secure_params
method (plus any more methods we might add beneath it).
Very simply, adding the private keyword restricts access to the secure_params
method so only methods in the same class can use it. You might be puzzled;
after all, how else could it be accessed? We havent explored calling methods from other classes, so Ill just say that without the private keyword, the
ROUTING
265
secure_params method could be used from code anywhere in our application. In this case, we apply the private keyword because we want to be
sure the secure_params method is only used in the ContactsController
class. Its just a bit of best practice and for now, you can simply learn that
secure_params method should be a private method.
Now lets look at routing for controllers that are built the Rails way.
Routing
Rails routing is aware of the seven standard controller actions.
In fact, it takes only one keyword (with one parameter) to generate seven different routes for any controller.
The keyword is resources and supplying a name that matches a model and
controller provides all seven routes.
Open the file config/routes.rb. Replace the contents with this:
Rails.application.routes.draw do
resources :contacts, only: [:new, :create]
root to: 'visitors#new'
end
[:new, :create].
[:new,
266
URI Pattern
/contacts(.:format)
/contacts/new(.:format)
/
/pages/*id
Controller#Action
contacts#create
contacts#new
visitors#new
high_voltage/pages#show
The output of the rake routes command shows weve created the routes we
need.
Our new route new_contact_path can now be used. Weve completed our
move to the model-view-controller architecture by adding the appropriate routes.
267
$ rails server
268
Git
Lets commit our changes to the Git repository and push to GitHub:
$ git add -A
$ git commit -m "contact form"
$ git push
Chapter 20
Send Mail
Email sent from a web application is called transactional email. As a website
visitor, youve probably seen transactional email such as these messages:
sign up confirmation email
response to a password reset request
acknowledgment of a purchase
notice of a change to a user profile setting
A web application can send email to a visitor. It can also send messages to its
owner or webmaster. On large active sites, email notices can be impractical (an
admin interface is better) but for our small-volume tutorial application, it makes
sense to email the contact request directly to the site owner (Foobar Kadigan is
retired and enjoys receiving email).
User Story
Lets plan our work with a user story:
269
270
To implement the user story, lets create a feature that sends the contact data as
an email message.
Implementation
Rails makes it easy to send email. The ActionMailer gem is part of any Rails
installation.
Implementation of email closely follows the model-view-controller architecture. To implement email, youll need:
model
view
mailer
The mailer is similar to a controller, combining data attributes from a model
with a view file. Any methods we add to the mailer class can be called from a
controller, triggering delivery of an email message.
The model can be any weve already created. In this case, well use the Contact model, since it gives us access to the visitors name, email address, and
message.
Well create a mail-specific view file in the app/views/user_mailer/ folder.
Our folder for mail-specific views will go in the app/views/ directory as a sibling of the app/views/layouts folder.
271
The Rails directory structure already gives us a folder app/mailers/ for the
mailer class and, not surprisingly, it is a sibling of the app/controllers/ folder.
We dont have to create the necessary folders and files manually, as the rails
generate command runs a utility to create what we need.
The name of the mailer isnt important; well use UserMailer because it is
obvious.
The rails generate command will create several files:
app/mailers/application_mailer.rb
app/mailers/user_mailer.rb
app/views/layouts/mailer.html.erb
app/views/layouts/mailer.text.erb
It also creates test files which we wont use in this tutorial.
This implements our model-view-mailer architecture.
272
def contact_email(contact)
@contact = contact
mail(to: Rails.application.secrets.owner_email, from: @contact.email, :subject => "Website C
end
end
273
tact messages in his inbox. You can see our use of the email attribute from the
Contact model in the expression from: @contact.email.
Thats all we need for mailer class. Next well create a view containing the
message.
274
You can easily imagine how this view would look as a web page. Youll soon
see it as an email message in your inbox.
For those recipients who like plain text, create a view without HTML markup.
Create a file app/views/user_mailer/contact_email.text.erb:
You received a message from <%= @contact.name %> with email address <%= @contact.email %>.
The visitor said:
"<%= @contact.content %>"
Modify Controller
Well add code to the ContactsController:
UserMailer.contact_email(@contact).deliver_now
275
def create
@contact = Contact.new(secure_params)
if @contact.valid?
UserMailer.contact_email(@contact).deliver_now
flash[:notice] = "Message sent from #{@contact.name}."
redirect_to root_path
else
render :new
end
end
private
def secure_params
params.require(:contact).permit(:name, :email, :content)
end
end
276
If you didnt get an email message in your inbox, make sure you set your config/environments/development.rb file to perform deliveries as described in
the Configuration chapter. Be sure to restart your server if you change the
configuration file.
Troubleshooting
If you get an error, you can practice troubleshooting. Youve set up a complex
system with many dependencies. Its great if it works, but there are several
opportunities for errors.
The most likely errors are a missing user name or password. We are trying
to connect to the SendGrid email service. SendGrid expects your SendGrid
user name. Mine is DanielKehoe (it is not my email address). SendGrid also
expects a password.
$ echo "$SENDGRID_USERNAME"
TROUBLESHOOTING
277
Password Issues
If you get the error message, SMTP-AUTH requested but missing secret phrase,
SendGrid is not receiving the SendGrid password.
Check that the SendGrid password is set in your Unix environment variables:
$ echo "$SENDGRID_PASSWORD"
You should see the long cryptic string in the console response. Again, make
sure youve used underscores consistently, and SENDGRID_PASSWORD is used
for the Unix environment variable as well as the config/secrets.yml file.
278
with:
development:
email_provider_username: example
email_provider_password: 's#cr*t'
In a YAML file, you do not need quotes unless your string contains special
characters. If your password contains any of these characters you should surround the string with single quotes:
: { } [ ] & * # ? | - < > = ! % @ \
Remember the security rule: Dont commit the config/secrets.yml file to Git
if it contains any secrets. Test the application and finish your troubleshooting. Then remove the hardcoded values from the config/secrets.yml file before
committing to Git.
Asynchronous Mailing
You may notice a delay in the responsiveness of the Contact form after adding
the email feature. Unfortunately, theres a performance penalty with our new
feature. Our controller code connects to the SendGrid server and waits for a
response before it renders the home page and displays the acknowledgment
message.
The performance penalty can be avoided by changing the implementation so
that the controller doesnt wait for a response from the SendGrid server. We
call this asynchronous behavior because sending email does not need to be
in sync with displaying the acknowledgment. Eliminating a delay improves
the user experience and makes the site feel more responsive. Asynchronous
mailing requires a queueing system for background jobs.
GIT
279
For our tutorial application, and for a typical small business website, the delay
caused by lack of queueing is no big deal. Keep in mind, though, as you tackle
bigger projects in Rails, you will need to implement a queueing system. Rails
includes the Active Job feature for background processing. The Mailing List
with Active Job tutorial in the Capstone Rails Tutorials series explains how to
use it.
Git
Lets commit our changes to the Git repository and push to GitHub:
$ git add -A
$ git commit -m "sending mail"
$ git push
Youve created a Rails application that handles a form and sends email to the
site owner.
Mail is a practical way to connect with site visitors. Lets implement a feature
that collects email addresses for mass mailing of a newsletter.
280
Chapter 21
Mailing List
Even as other messaging avenues become increasingly popular, such as messaging apps or Facebook messages, email remains the most practical way to
stay in touch with website visitors. Encouraging a visitor to provide an email
address means offering an invitation to a dialog and a relationship beyond a
single visit.
If you have a legitimate reason to stay in touch, and youve motivated the visitor
to leave an email address, youll need a mailing list service. Youve seen how
Rails can send an email message. From what youve seen so far, you can imagine it would not take much code to loop through a list of email addresses from
a database, sending a message to each. In the early days of the web, it was easy
for any system administrator to write a script for mass mailings. Since there is
negligible cost to sending bulk email, unscrupulous and ignorant operators sent
email to any address they could scrape, borrow, or steal. The resulting flood
of spam made checking ones inbox an icky experience and destroyed much of
the early culture of the Internet. Fortunately, services such as Gmail arose to
filter email. There is now a thick (but leaky) layer of screening protocols that
redirect spam to a junk folder. One reason you wont use a Rails application
to send bulk email is that a web application server is not the most efficient tool
for sending email. More significantly, theres a good chance your email wont
go through or, if it does (and someone complains), youll quickly see your IP
281
282
address blacklisted. Thats why we use mailing list services to send bulk email
such as newsletters or promotional offers.
Considerable expertise is required to keep email from being filtered as spam
(see MailChimps article Email Delivery For IT Professionals. Email service
providers increase reliability of delivery. These services track deliveries and
show how well your email is being delivered. Youll also get features such
as management of unsubscribe requests and templates to design attractive
messages.
There are at least a dozen well-established email service providers that allow
a Rails application to programmatically connect to the service (via an API) to
add or remove email addresses. For a list, see the article Send Email with Rails.
For this tutorial application, well use MailChimp because there is no cost to
open an account and you can send up to 12,000 emails/month to list of 2000 or
fewer subscribers for free.
Spam is unsolicited email. Dont ever send spam, whether for yourself, a client,
or an employer. If recipients complain, your IP address and domain name will
be blacklisted. So be very careful to only send to subscribers who signed up,
send what subscribers expect, and be sure to offer value. If you get complaints,
or the unsubscribe rate is high, stop.
Well assume weve discussed the rules with Foobar Kadigan and he is eager
to offer a newsletter to his visitors that will be genuinely appreciated.
User Story
Lets plan our work with a user story:
IMPLEMENTATION
283
Implementation
Well use the Rails model-view-controller architecture. Well need:
Visitors model
view for visitors#new
Visitors controller with new and create methods
routing for visitors#new and visitors#create
Well add a Visitor model that has a data attribute for an email address. We
already have a Visitors controller that renders the home page using the file in
the app/views/visitors/ folder. Well replace the contents of the view file with
a nice photo, a marketing message, and a form.
Our Visitors controller new and create methods will be very similar to what
we created for the Contacts controller. Instead of connecting to SendGrid to
send a message, well call a method to save the visitors email address to a
MailChimp mailing list.
Gibbon Gem
The Gibbon gem is a convenient wrapper for the MailChimp API. We could
connect to the MailChimp API using other gems that provide low-level plumbing such as HTTP connections (httparty) and data parsing (multi_json), but
other developers have already done the work of wrapping the plumbing in a
higher-level abstraction that easily fits into a Rails application. Amro Mousas
Gibbon gem is popular and actively maintained.
284
Home Page
Earlier we built a home page that provided a simple demonstration of the Ruby
language. Well discard it and replace it with a page that you could adapt for a
typical small-business website.
We want a nice photo, space for a marketing message, and the sign up form.
Replace the contents of the file app/views/visitors/new.html.erb:
<% content_for :title do %>Foobar Kadigan<% end %>
<% content_for :description do %>Website of Foobar Kadigan<% end %>
<section>
<img src="http://lorempixel.com/1170/600/cats/1">
</section>
<section>
<div class="column">
<h3>Stay in touch.</h3>
</div>
<div class="column">
<div class="form-centered">
<%= simple_form_for @visitor do |f| %>
<%= f.error_notification %>
<%= f.input :email, label: false, :placeholder => 'Your email address...' %>
<br/>
<%= f.button :submit, "Sign up for the newsletter", :class => "submit" %>
<% end %>
</div>
</div>
</section>
We include content_for view helpers that pass a title and description to the
application layout.
HOME PAGE
285
We add a photo to the page with an <img> tag. Were taking a shortcut and
using a placeholder photo from the lorempixel.com service.
The section and <div class="column"> tags apply a grid from Zurb
Foundation to create a row with two columns, one for our marketing message,
and one for the form.
Our marketing message is merely a placeholder. For a real website, youd likely
craft a stronger call to action than merely Stay in touch.
The form is very similar to the form on the Contact page, except we initialize it
with the @visitor instance variable and only need a field for an email address.
We suppress display of the email field label with the flag label: false and
use the :placeholder parameter to create a hint in the empty input field.
A submit element will contain the text, Sign up for the newsletter, and we
apply a CSS class to style the element as a button.
Photo Options
Youre free to modify this page as you wish, as long as you keep the form intact.
You might wish to modify the placeholder photo. If you dont like cats, try
http://lorempixel.com/1170/600/nightlife/1 or any other categories from the lorempixel.com service. You can change the size by modifying the dimensions from
1170 (pixel width) by 600 (pixel height).
You can replace the placeholder photo with your own. Look for the app/assets/images
folder and add an image. Instead of the HTML <img> tag, use the Rails
image_tag view helper, like this:
286
Visitor Model
The Visitor model is almost identical to the Contact model we created earlier,
except there is just one data attribute for the email field.
Well also add a subscribe method to add a visitor to a MailChimp list. Well
call this method from the controller when we process the submitted form.
Create a file app/models/visitor.rb:
class Visitor
include ActiveModel::Model
attr_accessor :email, :string
validates_presence_of :email
validates_format_of :email, with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i
def subscribe
mailchimp = Gibbon::Request.new(api_key: Rails.application.secrets.mailchimp_api_key)
list_id = Rails.application.secrets.mailchimp_list_id
result = mailchimp.lists(list_id).members.create(
body: {
email_address: self.email,
status: 'subscribed'
})
Rails.logger.info("Subscribed #{self.email} to MailChimp") if result
end
end
When you copy this, be careful to keep the long regex expression with:
/.../i on one line (no line breaks).
Just as we did for the Contact model, we use include ActiveModel::Model
to mix in behavior from the ActiveModel class. This is the best way to create a
model that does not use a database. In other applications, where models use a
database, you will create a model class that inherits from ActiveRecord instead.
We create the email attribute using the attr_accessor keyword, specifying
the email attribute will be a string. We set validation requirements using
validates_presence_of and validates_format_of keywords.
To subscribe a visitor to a mailing list, you need to provide:
VISITORS CONTROLLER
287
Visitors Controller
We already have a Visitors controller that contains a simple new method. Well
change the new method, add a create method, and provide a secure_params
private method to secure the controller from mass assignment exploits.
288
Our new method now assigns the Visitor model to an instance variable instead
of the Owner model.
The create method is almost identical to the Contacts controller create
method. We instantiate the Visitor model with scrubbed parameters from the
submitted form.
If the validation check succeeds, we subscribe the visitor to the MailChimp
mailing list with the @visitor.subscribe method. All the work of connecting to MailChimp happens in the Visitor model.
If the validation check fails, we redisplay the home page (the new action).
CLEAN UP
289
Clean Up
We no longer use the Owner model, so we can delete the file app/models/owner.rb:
$ rm app/models/owner.rb
Routing
Our routing is now more complex. In addition to rendering the visitors# new
view as the application root (the home page), we need to handle the create
action. We can use a resourceful route as we did with the Contacts controller.
Open the file config/routes.rb. Replace the contents with this:
Rails.application.routes.draw do
resources :contacts, only: [:new, :create]
resources :visitors, only: [:new, :create]
root to: 'visitors#new'
end
[:new, :create].
[:new,
290
URI Pattern
/contacts(.:format)
/contacts/new(.:format)
/visitors(.:format)
/visitors/new(.:format)
/
/pages/*id
Controller#Action
contacts#create
contacts#new
visitors#create
visitors#new
visitors#new
high_voltage/pages#show
The output of the rake routes command shows weve created the routes we
need.
291
Git
Lets commit our changes to the Git repository and push to GitHub:
292
$ git add -A
$ git commit -m "mailing list"
$ git push
Chapter 22
Deploy
Youve been running the default Ruby WEBrick server on your local machine.
If you wanted, you could leave your computer running, set up a managed DNS
service, and your web application would be accessible to anyone. But even if
you wanted to leave your computer running 24 hours a day, youre probably
not a security expert, WEBrick isnt tuned to handle much traffic, and your
computer is distant from the interconnection hubs where most websites are
hosted. For these reasons, when we move a web application from development
to production, we deploy it to a web hosting service that provides a hosting
platform on a server located in a strategically-located data center.
Data centers offer colocation services, renting rack-mounted computers with
fast Internet connections that can be configured as web servers. In the early
days of the web, deploying a web application required system administration
skills to configure and maintain a web server. Today, some developers like to
set up their web servers from bare metal using virtual private servers from
Linode, Slicehost, Rackspace, Amazon EC2, or others. With sufficent skills
and study, they say there is a feeling of satisfaction from doing it yourself. But
not everyone wants to be a system administrator. Most Rails developers simply
use a hosted platform as a service (PaaS) provider such as Heroku, EngineYard,
OpenShift, Cloud Foundry, or Shelly Cloud.
You may already be using a shared web hosting service such as GoDaddy or
293
294
Heroku Costs
It costs nothing to set up a Heroku account and deploy as many applications as
you want. Youll pay only if you upgrade your hosting to accommodate a busy
website.
Heroku pricing is based on a measure of computing resources the company
calls a dyno. Think of a dyno as a virtual server (though it is not). Heroku
provides one dyno for every web application for free. For personal projects,
you can run your Rails application on a single dyno and never incur a charge.
A single dyno idles after one hour of inactivity, going to sleep until it receives
a new web request. For a personal project, this means your web application will
respond with a few seconds delay if it hasnt received a web request in over an
hour. After it wakes up, it will respond quickly to every browser request.
If you want your web application to respond to every request without delay, you
can run two dynos. Heroku charges $35 per month for a second dyno running
full time (a dyno is billed at $0.05/hour).
A single dyno can serve thousands of requests per second, but performance
295
depends greatly on your application. With the Ruby WEBrick server, Rails
processes only one request at a time. Heroku doesnt support WEBrick, but
as a default it supports Thin, a similar single-threaded, non-concurrent web
server. Serving a typical Rails application that takes 100ms on average to process each request, Thin can accommodate about 10 requests per second per
dyno, which is adequate for a personal project.
If traffic surges on your website and exceeds 10 requests per second, you can
scale up. First, you can use the Unicorn web server which handles concurrent
requests. Configuring Unicorn requires some expertise, but Heroku recommends it. Second, you can double the size of Herokus dynos to handle more
requests. Finally, you can buy more dynos, adding as many dynos as you need
to handle traffic. This is where convenience comes at a price. You wont need
system administration expertise to deploy a website on Heroku but youll pay
a premium to host a high-traffic site.
Heroku is ideal for hosting our application:
no system administration expertise is required
hosting is free
performance is excellent
For this tutorial application, we wont concern ourselves with the possibility
that the website may get a lot of traffic. Im sure youll join me in offering
hearty thanks to Heroku for providing a convenient service that beginners can
use for free.
Lets deploy!
296
test-driven development, he or she will have a complete test suite that confirms
the application runs as expected. Often the developer uses a continuous integration server which automatically runs the test suite each time the code is
checked into the GitHub repository.
We havent used test-driven development to build this application so no test
suite is available. Youve tested the application manually at each stage.
Gemfile
We need to modify the Gemfile for Heroku.
We add a group :production block for gems that Heroku needs:
pg - PostgreSQL gem
thin - web server
rails_12factor - logging and static assets
Heroku doesnt support the SQLite database; the company provides a PostgreSQL database. Though we wont need it for our tutorial application, we
must include the PostgreSQL gem for Heroku. Well mark the sqlite3 gem to
be used in development only.
Note that Heroku recommends Unicorn for handling higher levels of traffic
efficiently. Unicorn can be difficult to setup and configure, so well simple use
the default WEBrick server for our tutorial application.
297
On Heroku, Rails needs an extra gem to handle logging and serve CSS and
JavaScript assets. The rails_12factor gem provides these services.
Open your Gemfile and replace the contents with the following:
Gemfile
source 'https://rubygems.org'
ruby '2.3.0'
gem 'rails', '4.2.5'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
end
gem 'foundation-rails', '~> 5.5'
gem 'gibbon'
gem 'high_voltage'
gem 'simple_form'
group :development do
gem 'better_errors'
gem 'quiet_assets'
gem 'rails_layout'
gem 'sqlite3'
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
If youve got Rails 4.2.5, theres no need to make additional changes to the
Gemfile. If youve got a newer version of Rails, update the Gemfile.
We have to run bundle install because weve changed the Gemfile. The
gems weve added are only needed in production so we dont install them on
our local machine. When we deploy, Heroku will read the Gemfile and install
the gems in the production environment. Well run bundle install with the
-without production argument so we dont install the new gems locally:
298
Lets commit our changes to the Git repository and push to GitHub:
$ git add -A
$ git commit -m "gems for Heroku"
$ git push
Precompile Assets
The rails_12factor gem automatically runs the rake assets:precompile
command when you deploy to Heroku, so it isnt strictly necessary to precompile assets yourself. However, it is important to understand that assets must be
precompiled before deployment.
We compile assets so we have one file each for all the files in the folders
app/assets/javascripts/ and app/assets/stylesheets/. It makes our application
faster for the user.
In development mode, the Rails asset pipeline live compiles all CSS and
JavaScript files. Compiling assets adds processing overhead. In production,
a web application would be slowed unnecessarily if assets were compiled for
every web request. Consequently, assets must be precompiled before we deploy
our application to production.
When assets are precompiled, the Rails asset pipeline will automatically produce concatenated and minified application.js and application.css files from
files listed in the manifest files app/assets/javascripts/application.js and app/assets/stylesh
Heres how to precompile assets and commit to the Git repo (if you want to do
it manually):
$
$
$
$
299
rake assets:precompile
git add -A
git commit -m "assets compiled for Heroku"
git push
The result will be several files added to the public/assets/ folder. The filenames
will contain a long unique identifier that prevents caching when you change the
application CSS or JavaScript.
Again, the rails_12factor gem runs the rake assets:precompile command
when you deploy to Heroku, so you dont have to do it yourself.
To block all search engine spiders, remove the commenting from the User-Agent
and Disallow lines.
You can learn more about the format of the robots exclusion standard.
300
Humans.txt
Many websites include a robots.txt file for nosy bots so its only fair that you
offer a humans.txt file for nosy people. Few people will look for it but you can
add a file public/humans.txt to credit and identify the creators and software
behind the website. The HTML5 Boilerplate project offers an example file or
you can borrow from RailsApps.
To deploy an app to Heroku, you must have a Heroku account. Visit https://id.heroku.com/sig
to set up an account.
Be sure to use the same email address you used to configure Git locally. You
can check the email address you used for Git with:
$ git config --get user.email
Heroku Toolbelt
Heroku provides a command line utility for creating and managing Heroku
apps.
Visit https://toolbelt.heroku.com/ to install the Heroku Toolbelt. A one-click
installer is available for Mac OS X, Windows, and Linux.
The installation process will install the Heroku command line utility. It also
installs the Foreman gem which is useful for duplicating the Heroku production
environment on a local machine. The installation process will also make sure
Git is installed.
HEROKU CREATE
301
The Heroku command line utility will create SSH keys if necessary to guarantee a secure connection to Heroku.
Heroku Create
Be sure you are in your application root directory and youve committed the
tutorial application to your Git repository.
Use the Heroku create command to create and name your application.
$ heroku create myapp
Replace myapp with something unique. Heroku demands a unique name for
every hosted application. If it is not unique, youll see an error, name is already taken. Chances are, learn-rails is already taken.
302
If you dont specify your app name (myapp in the example above), Heroku
will supply a placeholder name. You can easily change Herokus placeholder
name to a name of your choice with the heroku apps:rename command
(see Renaming Apps from the CLI).
Dont worry too much about getting the perfect name for your Heroku app.
The name of your Heroku app wont matter if you plan to set up your Heroku
app to use your own domain name. Youll just use the name for access to
the instance of your app running on the Heroku servers; if you have a custom
domain name, youll set up DNS (domain name service) to point your domain
name to the app running on Heroku.
The heroku create command sets your Heroku application as a Git remote
repository. That means youll use the git push command to deploy your
application to Heroku.
Enable Email
Youll need to enable email for production or else youll get errors when your
application tries to send email from Heroku.
To use SendGrid, add the following to your config/environments/production.rb
file:
# email enabled in production
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 587,
domain: Rails.application.secrets.domain_name,
authentication: "plain",
user_name: Rails.application.secrets.email_provider_username,
password: Rails.application.secrets.email_provider_password
}
# ActionMailer Config
config.action_mailer.default_url_options = { :host => Rails.application.secrets.domain_name }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
303
You can use port 25, 587, or 2525 (some ISPs restrict connections on port 25).
Be sure to add the new settings before the end keyword in the file. The settings
can be added anywhere, as long as they precede the end keyword!
Youll need to specify the unique name youve selected for your hosted application. Were using the Rails.application.secrets.domain_name
configuration variable in two places in the file. The config/secrets.yml file sets
configuration variables for use in production, which are obtained from Heroku
environment variables.
Be sure to commit your code to the Git local repository:
$ git add -A
$ git commit -m "email set for Heroku"
$ git push
When you set myapp.herokuapp.com, replace myapp with the name that
Heroku is using for your application. If you want to use a custom domain name,
youll need to set up DNS (domain name service), which we wont cover in this
tutorial.
304
You dont need to set SECRET_KEY_BASE, even though it is in your config/secrets.yml file. Heroku sets it automatically.
Check that the environment variables are set with:
$ heroku config
See the Heroku documentation on Configuration and Config Vars and the article
Rails Environment Variables for more information.
Push to Heroku
After all this preparation, you can finally push your application to Heroku.
Be sure to commit any recent changes to the Git local repository before you
push to Heroku.
You commit your code to Heroku just like you push your code to GitHub.
Heres how to push to Heroku:
$ git push heroku master
You may see a message, The authenticity of host heroku.com cant be established. Are you sure you want to continue connecting (yes/no)?. You can
answer yes and safely continue.
The push to Heroku takes several minutes. Youll see a sequence of diagnostic
messages in the console, beginning with:
-----> Ruby app detected
305
git add -A
git commit -m "revised application"
RAILS_ENV=production rake assets:precompile
git add -A
git commit -m "assets compiled for Heroku"
git push
git push heroku master
306
If youve configured everything correctly, you should be able to sign up for the
newsletter and send a contact request.
Customizing
For a real application, youll likely want to use your own domain name for your
app.
See Herokus article about custom domains for instructions.
You may also want to improve website responsiveness by adding page caching
with a content delivery network such as CloudFlare. CloudFlare can also provide an SSL connection for secure connections between the browser and server.
Heroku offers many add-on services. These are particularly noteworthy:
Adept Scale - automated scaling of Heroku dynos
New Relic - performance monitoring
For an in-depth look at your options, see the Rails Heroku Tutorial.
Troubleshooting
When you get errors, troubleshoot by reviewing the log files:
$ heroku logs
If necessary, use the Unix tail flag to monitor your log files. Open a new
terminal window and enter:
307
$ heroku logs -t
308
Chapter 23
Analytics
In earlier chapters, we built the tutorial application and deployed it for hosting
on Heroku.
Weve left something out. Though not obvious, its very important: analytics.
Analytics services provide reports about website traffic and usage.
Youll use the data to increase visits and improve your site. Analytics close
the communication loop with your users; your website puts out a message and
analytics reports show how visitors respond.
Google Analytics is the best known tracking service. It is free, easy to use,
and familiar to most web developers. In this chapter well integrate Google
Analytics with the tutorial application.
There are several ways to install Google Analytics for Rails. The article on
Analytics for Rails looks at various approaches and explains how Google Analytics works.
For this tutorial, well use the Segment.io service. The service provides an
API to send analytics data to dozens of different services, including Google
Analytics.
309
310
Segment.io
Segment.io is a subscription service that gathers analytics data from your application and sends it to dozens of different services, including Google Analytics.
The service is free for low- and medium- volume websites, providing 100,000
API calls (page views or events) per month at no cost. There is no charge to
sign up for the service.
Using Segment.io means you install one JavaScript library and get access to reports from dozens of analytics services. You can see a list of supported services.
The company offers helpful advice about which analytics tools to choose from.
For low-volume sites, many of the analytics services are free, so Segment.io
makes it easy to experiment and learn about the available analytics tools. The
service is fast and reliable, so theres no downside to trying it.
311
Youll check your Google Analytics account later to verify that Google is collecting data.
312
};
// For each of our methods, generate a queueing method.
for (var i = 0; i < window.analytics.methods.length; i++) {
var method = window.analytics.methods[i];
window.analytics[method] = window.analytics.factory(method);
}
// Define a method that will asynchronously load analytics.js from our CDN.
window.analytics.load = function (apiKey) {
// Create an async script element for analytics.js based on your API key.
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = ('https:' === document.location.protocol ? 'https://' : 'http://') +
'd2dq2ahtl5zl1z.cloudfront.net/analytics.js/v1/' + apiKey + '/analytics.min.js';
// Find the first script element on the page and insert our script next to it.
var firstScript = document.getElementsByTagName('script')[0];
firstScript.parentNode.insertBefore(script, firstScript);
};
// Add a version so we can keep track of what's out there in the wild.
window.analytics.SNIPPET_VERSION = '2.0.8';
// Load analytics.js with your API key, which will automatically load all of the
// analytics integrations you've turned on for your account. Boosh!
window.analytics.load('YOUR_API_TOKEN');
// Make our first page call to load the integrations. If you'd like to manually
// name or tag the page, edit or move this call to use your own tags.
/* */
window.analytics.page();
// accommodate Turbolinks
// track page views and form submissions
$(document).on('ready page:change', function() {
console.log('page loaded');
analytics.page();
analytics.trackForm($('#new_visitor'), 'Signed Up');
analytics.trackForm($('#new_contact'), 'Contact Request');
})
If you find you cant copy this code from this page, you can get it directly from
Segment.io , or use a file from the tutorial application on GitHub.
The Segment.io website offers a minified version of the snippet for faster page
loads. Weve used the non-minified version so you can read the code and com-
313
ments. If you want, you can get minified version from the Segment.io website
for improved speed.
You must replace YOUR_API_TOKEN with your Segment.io API token. You
can find the API token on your Settings page when you log in to Segment.io
(it is labelled Your API Key). Add it to the file where you see this line:
// Load analytics.js with your API key, which will automatically load all of the
// analytics integrations you've turned on for your account. Boosh!
window.analytics.load('YOUR_API_TOKEN');
Weve added extra code to the minified Segment.io JavaScript snippet. The
extra code accommodates page view and event tracking, which well look at
next.
314
// accommodate Turbolinks
// track page views and form submissions
$(document).on('ready page:change', function() {
.
.
.
analytics.page();
.
.
.
})
Event Tracking
Segment.io gives us a convenient method to track page views. Page view tracking gives us data about our website traffic, showing visits to the site and information about our visitors.
Its also important to learn about a visitors activity on the site. Site usage data
helps us improve the site and determine whether we are meeting our business
goals. This requires tracking events as well as page views.
The Segment.io JavaScript library gives us two methods to track events:
trackLink
trackForm
Link tracking can be used to send data to Segment.io whenever a visitor clicks a
link. It is not useful for our tutorial application because we simply record a new
SEGMENT.IO INTEGRATIONS
315
page view when a visitor clicks a link on our site. However, if you add links to
external sites and want to track click-throughs, you could use the trackLink
method. The method can also be used to track clicks that dont result in a new
page view, such as changing elements on a page.
The trackForm method is more useful for our tutorial application. Weve
already appended it to the app/assets/javascripts/segmentio.js file:
// accommodate Turbolinks
// track page views and form submissions
$(document).on('ready page:change', function() {
console.log('page loaded');
analytics.page();
analytics.trackForm($('#new_visitor'), 'Signed Up');
analytics.trackForm($('#new_contact'), 'Contact Request');
})
Segment.io Integrations
After installing the Segment.io JavaScript snippet in your application, visit the
Segment.io integrations page to select the services that will receive your data.
316
When you log in to Segment.io you will see a link to Integrations in the
navigation bar.
Each service requires a different configuration information. At a minimum,
youll have to provide an account identifier or API key that you obtained when
you signed up for the service.
For Google Analytics, enter your Google Analytics tracking id. It looks like
UA-XXXXXXX-XX.
Click Dashboard in the navigation bar so you can monitor data sent to Segment.io from your application.
When you test the application locally, you should see the results of page visits
and form submissions within seconds in the Segment.io Dashboard.
With Google Analytics enabled as a Segment.io integration, youll see form
submissions appear in the Google Analytics Real-Time report, under the Events
heading.
Note that Google doesnt process their data in real-time in most of its reports.
Data appears immediately in the Google Analytics Real-Time report. Other
Google Analytics reports, such as the Audience report, wont show data immediately. Check the next day for updated reports.
Deploy
When you are ready to deploy to Heroku, you must recompile assets and commit to the Git repo:
$
$
$
$
$
$
git add -A
git commit -m "analytics"
rake assets:precompile
git add -A
git commit -m "assets compiled for Heroku"
git push
317
When you visit the site, you should see real-time tracking of data sent to Segment.io in the Segment.io dashboard.
Log into your Google Analytics account to see real-time tracking of visits to
your website. Under Standard Reports see Real-Time Overview. Youll
see data within seconds after visiting any page.
318
Conversion Tracking
You may only be interested in knowing that people visit your site, without
measuring visitors engagement or response to the site. But in most cases, if
you build a website, youll offer a way for visitors to respond, whether it is by
purchasing a product, signing up for a newsletter, or clicking a like button.
The ultimate measure of website effectiveness is the conversion rate. The term
comes from the direct marketing industry and originally referred to a measure
of how people responded to junk mail offers. For a website, the conversion
rate indicates the proportion of visitors who respond to a call to action, which
may be an offer to make a purchase, register for a membership, sign up for a
newsletter, or any other activity which shows the visitor is engaged and interested.
For our tutorial application, we can measure our website effectiveness by looking at the conversion rate for newsletter sign-ups.
Were tracking page views which will give us a count of visits to the website
home page. And weve got event tracking in place to count newsletter sign-ups.
If 100 people visit the home page and 10 people request a newsletter, weve got
a conversion rate of 10%.
We can try to improve the conversion rate by improving the user experience
(perhaps through A/B testing) or focusing on increasing traffic from sources
that provide a higher conversion rate.
You can monitor your sites conversion rate by setting up events as goals in
Google Analytics. Segment.io also integrates with many services which provide conversion tracking.
319
If your project was to build an application for someone else, whether the company you work for, or a client like Foobar Kadigan, youve completed the deliverable.
You started with project planning, in the form of user stories. You implemented
the application using a variety of technologies supported by the Ruby on Rails
development platform. And youve deployed the application for others to use,
with analytics in place to track traffic and usage.
Not every manager or client will appreciate the effort or the complexity of the
project youve built. Mr. Kadigans happiness may depend on how well youve
understood his goals and the degree to which youve met his expectations. If
youre working for yourself, or launching your startup, you may be your own
toughest boss, because there is always more to do.
With technology projects, like many other aspects of life, though it seems youll
never get it right, and never get it done, there are moments when you can savor
a sense of accomplishment. This is one of those moments.
Before you start thinking about adding one more feature, or updating the application for the new releases that inevitably came out during the time you were
working, take time to bask in the satisfaction of seeing the results of your work.
Software development has its own unique rhythm of frustration and satisfaction. As software developers, we subject ourselves to hours, days, or weeks of
struggle with code that is cryptic and resists understanding. We gain mastery
for a few minutes and then turn to the next problem. With each feature you implement, or issue you resolve, youll experience brief elation before resuming
the grind of development. But at each milestone, and at the completion of the
project, youve built something tangible that you can use. You can try it out
yourself and show it to others.
Give yourself full credit. Youve built something extraordinary with little more
than intelligence and attention. Youve leveraged the work of other developers
who have contributed to the open source Ruby on Rails platform and youve
created your own unique product. This is what drives us as developers; to create
something from nothing, using only our collective intelligence and ambition.
320
Chapter 24
Testing
You dont need to read this chapter if you will always be a student, or a hobbyist, working on personal projects. But if you wish to work as a professional
Rails developer, or launch your own startup, with money and reputation at
stake, you must learn about testing. In this chapter, Ill introduce the basic
concepts of testing and show how to build a test suite for the tutorial application.
Why Test?
Software applications are fragile. When you write a song, you can include a
wrong note and the song wont break. In a film, technical flaws like a jump
cut or a microphone in the frame wont ruin an entire movie. But an unseen
error in a software program can disrupt a key feature or crash the entire application.
Software applications are never finished. Songs and movies reach a stage of
completion and are delivered to their audience, with no expectation that the
completed work will change. With software applications, theres always an
upcoming version with bug fixes or new features. As web developers, we continue to make changes to the applications that our customers are actively using.
321
322
SCRIPTED OR EXPLORATORY
323
for the methods of your tests, either Minitest or RSpec. Test code is different
from code that implements features in one significant way: Instead of supporting interactions with a user, test code interacts with the code youve written,
verifying the code behaves as intended.
Scripted or Exploratory
When testing is used for quality assurance, the goal is to create a suite of automated tests that will reveal any bugs that creep into code and break the application. Sometimes this is called scripted testing. These tests are checked into the
software repository and maintained with the application. Often developers will
set up a system for continuous integration (CI), which will automatically run
the test suite whenever the repository is updated. Developers can set up a CI
server such as Jenkins or use a hosted CI service such as Travis CI, CircleCI,
or Semaphore to run tests automatically. Automated testing with continuous
integration serves as a safety net for developers.
There is another role for testing, which is often called exploratory testing, or
developer testing. These tests may end up in an application test suite, but the
primary purpose is to help a developer construct an application. Many developers, after gaining experience in writing tests for quality assurance, realize
that writing tests can be a useful first step in figuring out how a feature should
be implemented. It may seem odd to write tests first, but exploratory testing can clarify what behavior will be required, and help the developer think
through alternatives and edge cases. This approach is called test-first development, and many developers will tell you that when you write tests first, youll
be more satisfied; youll be more focused; and youll avoid tangents and detours of the nice-to-have-but-not-really-needed variety. Well look closely
at test-first development in conjunction with Test-Driven Development (TDD)
and Behavior-Driven Development (BDD) at the end of this chapter. First, lets
gain an understanding of testing terminology and practice.
324
SAMPLE DATA
325
Unit tests probe the internal workings of the machine. If weve written our code
well, a small section of the code, such as a class or a method, will be a discrete
unit that can be tested independently of all other units. Unit tests inspect the
integrity of small parts of the application in isolation. When a unit test fails,
we can quickly identify and fix broken code.
We use integration tests to make sure the entire application works as expected.
Integration tests mimic the behavior of real users. For a web application, an integration test may simulate a user signing in, filling out forms, clicking between
pages, and verifying that contents of pages match expected results. Integration
tests can also be called feature tests if they are designed to confirm that product
features work as expected. Our feature tests can serve as acceptance tests if we
use the test suite to determine if weve correctly implemented our user stories
or other product specifications. Sometimes these tests are called black box tests
because the code is tested as if the application was a black box, with the internal workings of the application hidden from the observer. They are also called
system tests or end-to-end tests.
Sample Data
When we write tests, either feature tests or unit tests, we often want to check
whether a method returns the data we expect. That means we have to create
the data we need in advance of the test. Either we populate a database with
the data we expect, or we disconnect the database and instantiate an object
that provides the data we expect. Test frameworks give us a tool named a
factory or a fixture to create sample data. Developers argue about what is better,
factories or fixtures, but youll encounter factories more often, particularly the
popular FactoryGirl gem. A factory is an object that creates other objects.
When you use FactoryGirl, you have the option of saving your object to the
database (which is slow) or building your object in memory only (which is
faster). Fixtures are used to populate a database with sample data before your
tests run. If you use fixtures, youll save sample data in a configuration file.
Before tests run, Rails automatically loads all fixtures from configuration files
326
in the test/fixtures folder. As you gain experience with testing, youll become
familiar with both factories and fixtures.
Test Doubles
In unit testing, to isolate small parts of the application, sometimes we artificially decouple the code from the rest of the application. For example, with a
unit test, we dont want to connect to an external service with an API to obtain
data. Or we simply want a method to get a predictable response from another
object.
Test doubles stand in for external dependencies. The term is borrowed from
Hollywood, where stunt doubles stand in for actors in action scenes. A test
double is any kind of pretend object used in place of a real object for testing
purposes. There are two types of test doubles, stubs and mocks. Stubs provide
canned answers to calls made during the test, only responding when queried
by the test. Sometimes stubs record information about the call, for example,
the message sent or the number of times called. Mocks are pre-programmed
objects that reproduce the behavior of the object they represent, forming a specification of an objects behavior. It takes time to write stubs and mocks and lots
of experience to use them correctly, so as a beginner, you probably wont write
stubs and mocks without help. As you can gain experience, youll better understand the difference between stubs and mocks and learn how to use them.
For now, it is enough to recognize the terminology and remember that tests run
faster and better when we reduce coupling and complexity with test doubles.
327
often stray from the default Rails stack when it comes to testing. Since the
release of Ruby 1.9, Minitest has been supplied as a standard gem with all
Ruby installations. Yet most Rails developers use RSpec for testing.
In this tutorial, Ill use Minitest to introduce you to testing. Minitest is easier
to set up and offers a syntax that is very similar to RSpec. Some developers
say that there is no reason to use RSpec because Minitest provides almost all
the convenience of RSpec with smaller size, faster speed, and less complexity.
Other developers insist that RSpec is more expressive and flexible. Realistically, if you want a job working on most Rails teams, youll need to learn
RSpec. Get started with Minitest to learn the basics of testing. When youre
ready for the next step, Ive written an RSpec Tutorial to take you deeper. I also
recommend the books Rails 4 Test Prescriptions by Noel Rappin and Everyday
Rails Testing with RSpec by Aaron Sumner.
328
interacts with the web application, via calls to Rack, as if it was a browser
making requests and receiving HTML files as a response.
When we use Capybara, by default it operates in headless mode, interacting
directly with the Rails application via Rack. Headless means there is no
graphical user interface (as if the absent screen was a computers head). In
headless mode, JavaScript is unavailable. If some of our application features
require JavaScript, we must set up Capybara to act as a robot using a real web
browser. Capybara has a built-in driver (named Selenium) that gives our robot
the option of automatically launching and using a real web browser for each
test. By default, Capybara will use the Firefox web browser if it is installed
on your computer. What youll see is amazing. When you run tests using
Capybara with the JavaScript option, the Firefox web browser will pop open
on your desktop and youll watch a ghost flying through your web application.
With Capybara, you now have a ghostly QA department running your integration tests.
329
During the exercise phase, something is executed. Often this is a method call:
user.save
During verification, the result of the exercise is verified against the developers
expectations:
330
user.email.must_equal '[email protected]'
During teardown, the system under test is reset to its initial state. Rails integrates with Minitest or RSpec to reset a database to its initial state. You will
seldom write code for the teardown phase.
Now that youve learned about the basic concepts of testing, lets set up Minitest
for our first tests.
Set Up Minitest
Well set up testing with both Minitest and Capybara, so we can write both
unit tests and feature tests. Minitest is a standard Ruby gem, installed when
you install Ruby in your environment. Well install the minitest-spec-rails gem
which makes it easy to use an RSpec-like syntax with Minitest. Well also add
the minitest-rails-capybara gem to integrate Capybara with Minitest and Rails.
Open your Gemfile and replace the contents with the following:
Gemfile
source 'https://rubygems.org'
ruby '2.3.0'
gem 'rails', '4.2.5'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
end
gem 'foundation-rails', '~> 5.5'
gem 'gibbon'
gem 'high_voltage'
gem 'simple_form'
SET UP MINITEST
group
gem
gem
gem
gem
end
group
gem
gem
end
group
gem
gem
end
331
:development do
'better_errors'
'quiet_assets'
'rails_layout'
'sqlite3'
:production do
'pg'
'rails_12factor'
:test do
'minitest-spec-rails'
'minitest-rails-capybara'
Weve added the two gems to the test group. Now, some gems are loaded
only when were writing code (during development), some are loaded only
when the application is running on Heroku (deployed to production), and our
newest additions only are loaded when we run tests. If youve got Rails 4.2.5,
theres no need to make additional changes to the Gemfile. If youve got a
newer version of Rails, update the Gemfile.
Next, install the additional gems:
$ bundle install
The bundle install command will download and install the gems from the
rubygems.org server.
332
Run Tests
The command rake test will execute Minitest. Lets see what happens when
we run tests:
$ rake test
Run options: --seed 9073
# Running:
Finished in 0.006803s, 0.0000 runs/s, 0.0000 assertions/s.
0 runs, 0 assertions, 0 failures, 0 errors, 0 skips
The output shows that Minitest executes but we have no tests for it to run.
Lets commit our changes to the Git repository and push to GitHub:
$ git add -A
$ git commit -m "set up minitest"
$ git push
333
require 'test_helper'
class VisitorTest < ActiveSupport::TestCase
def valid_params
{ email: '[email protected]' }
end
def test_valid
visitor = Visitor.new valid_params
assert visitor.valid?, "Can't create with valid params: #{visitor.errors.messages}"
end
def test_invalid_without_email
params = valid_params.clone
params.delete :email
visitor = Visitor.new params
refute visitor.valid?, "Can't be valid without email"
assert visitor.errors[:email], "Missing error when without email"
end
end
Notice that we must declare a class VisitorTest that inherits from ActiveSupport::Te
Then we must define a new method for each test case using the def keyword.
This syntax is not popular with Rails developers. RSpec offers its own DSL
(domain specific language) that hides the overhead of setting up classes and
methods behind convenience methods. Minitest offers its own version of the
the RSpec DSL, allowing us to use the more popular syntax. Ill use the new
RSpec-like syntax in this tutorial, since you are likely to encounter RSpec more
frequently.
334
The test above, written in the RSpec-like syntax, is functionally identical to the
previous example, written in the old test_unit syntax. Take a close look at both,
so the structure and keywords will be familiar when you see it again.
We need require test_helper to enable the test framework and apply
any configuration settings.
The keywords describe, let, and it are keywords that are also used in the
RSpec DSL (domain-specific language). When you see these keywords, you
know you are looking at test code, either Minitest or RSpec.
335
The purpose of a unit test is to describe the system under test, in terms of
its expected behavior. We create a do ... end block using the describe
keyword and specifying a class we wish to test:
describe Visitor do
.
.
.
end
The describe keyword creates a test class. In this case, the describe keyword will create a class named VisitorTest that inherits from ActiveSupport::TestCa
Using the old test_unit syntax, we could do this with class VisitorTest
< ActiveSupport::TestCase but the describe keyword is more convenient. When Minitest runs, it recognizes and executes test classes. By including
our code inside a test class, we get to use methods such as let and it which
are useful for writing tests. Minitest will recognize various classes like models
or controllers and provide appropriate behavior.
Setup Phase
We must set up everything we need for the test. Minitest provides a simple way
to set up everything before a test using the before keyword:
before do
do_some_setup
end
We could initialize the Visitor model using a before block and setting instance
variables:
336
before do
@visitor_params = {email: '[email protected]'}
@visitor = Visitor.new(visitor_params)
end
Instead of using a before block, well use the convenient let keyword:
let(:visitor_params) { {email: '[email protected]'} }
let(:visitor) { Visitor.new visitor_params }
Do It
Each test is defined by the it keyword and a do ... end block that contains
the exercise and verify phases of the test. The it keyword must be accompanied by a description. The description will be displayed if the test fails.
For our first test, we want to check if the Visitor model can be created when we
provide a valid email address. Before the test runs, the let statement makes
sure the Visitor object is instantiated with an email value.
The verification phase of each test consists of a comparison between the results
of an operation and our expectations. We expect that each time we create a
Visitor object with a valid email address, the visitor.valid? method will
return true. We can create a test:
it 'is valid when created with valid parameters' do
assert_equal visitor.valid?, true
end
337
The keyword assert_equal is the old test_unit syntax. It compares the result
of visitor.valid? with true and tells Minitest the test has passed or failed.
We can write the same thing using the new RSpec-style syntax:
it 'is valid when created with valid parameters' do
visitor.must_be :valid?
end
The method must_be is an expectation. You can see a Minitest cheat sheet
with a list of all the expectation methods. As you might guess, must_be functions as a comparison operator, checking if a call to visitor.valid? returns
true.
For our second test, we want to make sure the Visitor object is invalid when no
email address is provided:
it 'is invalid without an email' do
# Delete email before visitor let is called
visitor_params.delete :email
visitor.wont_be :valid? # Must not be valid without email
visitor.errors[:email].must_be :present? # Must have error for missing email
end
We created the visitor_params hash with a let statement. Before we invoke the Visitor object and call the visitor.valid? method, we delete the
email address from the visitor_params hash. When the Visitor object is
invoked, it will be created by the let statement without an email address. The
wont_be expectation confirms that the result of visitor.valid? method is
false. Then we check if a validation error message is present.
At this point, dont expect to be ready to write unit tests for every model
method. Youll need to spend time with the documentation for Minitest expectations or the Minitest cheat sheet to become familiar with all the possible
ways to write tests. This introduction should help you recognize the syntax of
tests, understand the structure, and give you the background you need to learn
more about unit testing.
338
Run Tests
Lets run our unit tests:
$ rake test
Run options: --seed 53300
# Running:
..
Finished in 0.028884s, 69.2425 runs/s, 103.8637 assertions/s.
2 runs, 3 assertions, 0 failures, 0 errors, 0 skips
RUN TESTS
339
When you copy this, be careful to keep the long regex expression (/\A. . . \z/i)
on one line (no line breaks).
Weve commented out the statements that require validation for the email attribute. Lets run the tests again:
$ rake test
Run options: --seed 34847
# Running:
.F
Finished in 0.013429s, 148.9314 runs/s, 148.9314 assertions/s.
1) Failure:
Visitor#test_0002_is invalid without an email [/Users/danielkehoe/workspace/wip/learn-rails/test
Expected # to not be valid?.
2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
The output shows a failure. The diagnostic message displays the description of
the failing test, Visitor#test_0002_is invalid without an email, and indicates
the line number where the test failed. Now you know what a failing test looks
like.
Before you continue, restore the file app/models/visitor.rb to its original state,
and make sure the tests pass.
If you wish, you can continue writing unit tests. You could create a similar
unit test for the Contact model. With more experience, or some independent
research, you could create a test for the subscribe method in the Visitor
model. This method connects to an external API, so it requires test doubles to
fake the response of the external services. Our goal here is to introduce you to
the concepts of testing, so well put aside advanced work on unit tests, and take
a look at feature tests.
340
Feature Test
Lets start with a user story for our home page. It might seem trivial to call
the home page a feature and describe it with a user story, but it illustrates a
process that works just as well with more complex features. Heres our user
story:
*Feature: Home page*
As a visitor
I want to visit a home page
And see a welcome message
For our test, we know we want to visit the home page and check if the words
Stay in touch appear on the page. This is the scenario well test:
*Scenario: Visit the home page*
Given I am a visitor
When I visit the home page
Then I see "Stay in touch"
FEATURE TEST
341
#
I want to visit a home page
#
So I can learn more about the website
feature 'Home page' do
# Scenario: Visit the home page
#
Given I am a visitor
#
When I visit the home page
#
Then I see "Welcome"
scenario 'visit the home page' do
visit root_path
page.must_have_content 'Stay in touch'
end
end
Ive included the user story and scenario description in comments. Theres no
convention to do so, but it will help you to see the relationship between testing
and the product planning process. It should be easy to transform a Given. . .
When. . . Then. . . scenario into the code needed for a feature test.
Feature
When we created a unit test, we used the describe keyword to create a
test class. The feature keyword creates a test class that inherits from the
Capybara::Rails::TestCase class, giving us methods such as visit and
page.
Feature tests are created with a do ... end block using the feature keyword and providing a description of the feature:
feature 'Home page' do
.
.
.
end
Notice that the description is placed in quotes. In this case, Minitest will automatically generate a class named HomePageTest.
342
Scenario
Typically we test a single feature with multiple scenarios in a single test file.
The scenario keyword is similar to the it keyword youve seen in unit tests.
Each feature test is defined by the scenario keyword and a do ... end
block that contains the visit and verify phases of the test. The scenario keyword must be accompanied by a description. The description will be displayed
if the test fails.
The directive visit is a Capybara method that takes a URL or Rails route as
an argument. You could specify either visit / or visit root_path to
direct Capybara to retrieve the home page.
Capybara provides other actions in addition to visit. You can see the documentation for Capybara actions that include actions for filling in a form and
clicking a button.
Capybara creates a page object for us as a response to the visit. The page
object is a representation of the HTML file returned by the application. We can
call the must_have_content method, testing if the string Stay in touch is
present in the page.
Capybara gives us a collection of methods we can use to verify our expectations. The documentation for Capybara expectations provides an extensive
collection of methods we can use to verify whats on a web page. For example,
must_have_link checks for a link. With Capybara expectations, you can
check almost anything on a page. Combining Capybara actions and expectations allows you to build a powerful page-checking robot.
RUN TESTS
343
Run Tests
Lets run all our tests:
$ rake test
Run options: --seed 15723
# Running:
...
Finished in 0.165429s, 18.1347 runs/s, 24.1796 assertions/s.
3 runs, 4 assertions, 0 failures, 0 errors, 0 skips
We have three tests (in two test files) making four assertions, all passing.
Troubleshooting
You might get an error message:
rake aborted!
NoMethodError: undefined method `feature' for main:Object
Youll see this error message if you neglected to modify the test/test_helper.rb
file to allow use of the Capybara test framework methods.
344
GO AWAY!
1) Failure:
Home page Feature Test#test_0001_visit the home page [/Users/danielkehoe/workspace/wip/learn-rai
Expected to include "Stay in touch".
3 runs, 4 assertions, 1 failures, 0 errors, 0 skips
The output shows a failure. The diagnostic message displays the description
of the failing test, Home page Feature Test#test_0001_visit the home page,
showing a failure, Expected to include Stay in touch.
Before you continue, restore the file app/view/visitors/new.html.erb to its
original state, and make sure the tests pass.
OTHER TESTS
345
Using Capybara
There is an art to developing feature tests. You can test that all the text on
the home page is exactly what you want. That would make your test files
large. And your tests would be brittle, because any changes you made in
development, even the slightest changes to the words on the page, would break
your tests. For good integration tests, focus on the features that are essential to
your application. For example, use the Capybara robot to make sure the user
can follow a critical path through your application, visiting important pages,
filling in forms, clicking buttons, and seeing results. Capybara lets you select
any HTML element on a page, so you can check an ID or class attribute of an
HTML tag, not just text on a page. Youll want to be confident that application
navigation and page flow continues to work after any code changes. That will
serve you better than tests that tell you a word changed here or there.
Other Tests
The art of testing lies in making good choices about what to test. Its common to write feature tests because they will test the entire application from the
viewpoint of the user. It is also common to write unit tests for models because
models contain much of the uniqueness of an application.
Every other aspect of a Rails application can be tested, including controllers,
helpers, and views. Developers seldom write tests for every aspect of a Rails
application. If your controllers contain only the standard RESTful actions, with
no extra logic, you probably dont need to write unit tests for your controllers.
If you only have simple HTML markup in helpers, helpers dont need to be
tested. And views are rarely tested with unit tests (use feature tests if you want
to make sure a page contains what you expect). As a beginner, youll make a
good start if you concentrate on unit tests for models and integration tests for
your page flow.
346
Behavior-Driven Development
In the Plan Your Product chapter, you learned about the software development approach called Behavior-Driven Development (BDD), or sometimes,
Behavior-Driven Design. In writing the feature tests for the home page, you
saw it in action. With BDD, you turn user stories into detailed scenarios that
are accompanied by tests. BDD is a powerful approach for managing software development. It helps you define your product requirements, refine your
project scope, and divide your project into well-defined tasks. The BDD process is complete when each feature has automated tests, when you enter rake
test on the command line and see that every feature is implemented and functioning as expected.
You may feel lost or overwhelmed when you attempt to build a Rails application for the first time, especially if your only experience is following the stepby-step instructions of a tutorial. When you experience that panic, BDD is your
lifeline. Start by writing user stories for a few simple features. Write feature
tests and implement the code required to make the tests pass. As you focus on
the process of writing scenarios and tests, and implementing the code for each
feature, youll begin to gain momentum, and before you know it, youll be over
the first hurdle.
Test-Driven Development
You can see how the BDD approach refines the product requirements and user
experience. At a microscopic level, a similar discipline, named test-driven
development, helps refine the implementation. Where BDD is driven by feature
tests, TDD is focused on unit tests.
TDD is an approach to software development that emphasizes that all code
should be written to be tested. Excellent test coverage, allowing easy refactoring, is not the only goal of TDD. Just as important, the developer focuses on
what needs to be accomplished and thinks through alternatives and edge cases.
TEST-FIRST DEVELOPMENT
347
Some TDD aficionados say testing is a tool to write better code, and regression
tests are a side effect. Unit tests are at the heart of TDD, and easiest to write
when code is carefully decoupled into systems that can be tested in isolation.
An application that is composed of decoupled units with clearly defined interfaces is a well-designed application that is easy to extend and maintain. If you
make it a practice to write unit tests in conjunction with all the code you write,
youll write better code, and youll be practicing TDD.
Test-First Development
Often when you are practicing TDD, youll write tests before you write implementation code. Earlier in this chapter, I referred to test-first development and
explained that it serves a different purpose than testing for quality assurance. In
some situations, test-first development is simply exploratory testing, a means
of describing the behavior of the code that must be built. Test-first development is particularly useful when youve solved a similar problem and know
exactly what results to expect, making it easy to write tests before writing the
implementation.
Test-first development leads to a red-green-refactor workflow. A developer
imagines the results of an operation, writes a test that checks for the results, and
runs tests which fail (with the right configuration, failing tests display as red in
the console). Then the developer writes code that produces the correct results
and runs the tests again, improving the code until the tests pass (displaying
in green). At this point, the developer has an adequate regression test and
can begin to refactor to improve the implementation, checking that the tests
continue to pass. Developers like the rhythm and coherency of the red-greenrefactor workflow. Writing tests creates discrete, manageable tasks. When
tests pass, turning green, there is a feeling of satisfaction, of a job well-done.
By postponing concerns about improving the code to a refactoring phase, its
easier to get the job done without trying to get it perfect. And perfection can be
pursued in the refactoring phase without worrying about regressing to a broken
state.
348
David Heinemeier Hansson, the creator of Rails, famously declared that TDD
is dead. Long live testing.:http://david.heinemeierhansson.com/2014/tdd-isdead-long-live-testing.html He pointed out that sometimes ardent advocates
of TDD will try out an implementation before writing tests, to determine what
needs to be done, or to clarify a problem. In the real world, even though developers recommend writing tests first, there are often times when a developer
will write tests only after writing code and settling on an approach. TDD,
which emphasizes the benefit of writing tests as a first step, doesnt really require that you write tests before you write code, or even that you write tests
for all code. The test-first emphasis of TDD is a recommendation, not a rule.
Youll be a better developer if you find opportunities to get in the zone with
the red-green-refactor workflow of test-first development, but testing is worthwhile whether it comes first or last.
Words of Encouragement
Testing often intimidates the newcomer. It is difficult to find good examples.
The syntax of Minitest and RSpec has evolved over time, so there is little consistency among examples youll find. Older examples are not a good guide
to current practices. But once you gain familiarity with the concepts in this
chapter, you can start writing tests.
Testing is one of the few things in Rails that you can jump into without getting
just right. You cant screw up your code base by writing incorrect tests. Experienced developers seem to worry that inexperienced developers will write
slow tests, but in truth, a slow test is better than no test. Tests wont affect the
performance of your application in production.
If your code is clumsy, dont worry, youll get better with practice. Whats
most important is that youve begun writing tests. Thats an indication you are
committed to Rails best practices.
Your tests are only bad if they dont cover your code adequately or if they
give you a false sense of assurance. You will only discover this over time, as
WORDS OF ENCOURAGEMENT
349
you find bugs you didnt anticipate (which is inevitable). Its better to just begin
testing, even if youre not sure youre doing it right, than to not test at all.
350
Chapter 25
Rails Composer
Im going to show you how to skip all the work you already did, and build
a ready-to-use Rails application in less than five minutes. When youre done
with this chapter, you may wonder why you read the rest of the book.
This chapter is about Rails Composer, a tool for building starter applications.
Rails Composer makes building applications so easy, it feels like a guilty pleasure.
In the introductory Create the Application chapter, you learned that developers often use a starter application instead of assembling an application from
scratch. Youve seen how the rails new command gives you a rudimentary
starter application. Developers typically add a front-end framework, a testing framework, and a handful of favorite gems before they get started on any
custom development. Since most applications start with the same basic components, it makes sense to rely on an open source effort to stitch them together,
so any integration issues or update problems are quickly resolved by the community. Thats the idea behind the RailsApps project. The project provides a
collection of starter applications, plus Rails Composer, a tool that creates the
starter applications.
Ive been leading the RailsApps project for several years because I think the
project is very important. I may be biased, so take a look and judge for yourself.
351
352
option
1)
2)
3)
choose
353
Options #2 and #3 are not for beginners. Well skip any contributed applications. And the Custom application option is strictly for experts. Enter 1 to
select Build a RailsApps example application. Youll see a list of available
starter applications:
option Choose a starter application.
1) learn-rails
2) rails-bootstrap
3) rails-foundation
4) rails-mailinglist-activejob
5) rails-omniauth
6) rails-devise
7) rails-devise-roles
8) rails-devise-pundit
9) rails-signup-download
10) rails-stripe-checkout
11) rails-stripe-coupons
choose Enter your selection:
Well explore the list later. For now, enter 1 to select learn-rails.
option
1)
2)
3)
choose
Heres your chance to get news and announcements about Rails Composer:
option
354
Either enter your email address (if you want news) or press return to skip it
(if youre shy).
Youll be asked:
option
Enter y or yes since you are using RVM. Rails Composer will create a new
gemset named foobar-kadigan. In less than the time it took me to write this
sentence, youll have a new Rails application. Look for it in your folder:
$ ls -1
foobar-kadigan
learn-rails
As soon as you move into the foobar-kadigan/ folder, RVM will automatically begin using the gemset named foobar-kadigan. Thats because Rails
Composer created hidden .ruby-gemset and .ruby-version files.
355
Rails Bootstrap
The rails-bootstrap application provides an integration of Rails and Bootstrap, the popular front-end framework. Youll recall that Bootstrap and Foun-
356
dation are very similar. This application gives you everything you built in this
books chapters on Layout and Views and Front-End Framework, including flash messages and navigation, set up for Bootstrap.
You can examine the example application on GitHub, in the rails-bootstrap
repository.
You can read the Bootstrap Quickstart Guide to understand the code.
Rails Foundation
The rails-foundation application is just like the rails-bootstrap application,
only with Foundation instead of Bootstrap. Its a stripped-down version of
the learn-rails application you just built, without the contact form or mailing
list sign-up. If you want to build a custom application, starting with nothing
more than Foundation and an about page, generate the rails-foundation
application.
You can examine the example application on GitHub, in the rails-foundation
repository.
Ive written a Foundation Quickstart Guide, but its nearly identical to what
youve already read in this book.
357
Rails OmniAuth
OmniAuth is a gem for authentication. Most web applications need a way
for users to sign in, allowing access to some features of the application only
for signed-in users. OmniAuth allows a user to sign in using an account they
already have with a service such as Facebook, Twitter, or GitHub. If youre
building an application that needs quick and easy sign-in, this is a useful starter
application.
You can examine the example application on GitHub, in the rails-omniauth
repository.
You can read the OmniAuth Tutorial to learn about authentication with OmniAuth.
Rails Devise
Devise is the most popular gem for authentication. Devise provides user management and authentication, letting a user sign up to create an account and log
in with an email address and password. Most websites need email/password
authentication, so this is a popular starter application.
You can examine the example application on GitHub, in the rails-devise repository.
You can read the Devise Quickstart Guide to learn about user management and
authentication with Devise.
358
359
the code in the Signup and Download Tutorial, you could customize the learnrails application so visitors could download an ebook by Foobar Kadigan after
they sign up for his newsletter.
Stripe is a popular service used to accept credit card payments. Stripe offers two approaches to implementing payment processing. Stripe Checkout is
Stripes entry-level approach. Stripe Checkout competes with the button-based
payment options from Google, PayPal, or Amazon, adding a pop-up payment
form to any web page. Stripe Checkout is very limited because the pop-up payment form cannot be customized for use with a Rails application. Our Stripe
Checkout Tutorial shows how to combine Stripe Checkout with Devise for simple applications.
Stripe.js is optimal for use with a Rails application, allowing full customization of a payment form and integration with Rails form processing. The railsstripe-coupons application implements a payment feature using Stripe JS so
a visitor pays to download a PDF file. The application accommodates promotional coupons and adds payment forms to landing pages, for real-world
payment processing. Our Stripe JS With Coupons tutorial provides the details.
360
$ cd ~/workspace
$ pwd
/Users/danielkehoe/workspace
Its okay to start with the learn-rails gemset. We have to start with a gemset
that already has the Rails gem installed. After that, Rails Composer will create
a new gemset for the new project.
$ rvm use ruby-2.3.0@learn-rails
Dont worry if some of the prompts are different from the ones I describe here.
Rails Composer changes often. At the time I wrote this, I saw:
option
1)
2)
3)
choose
361
Select rails-devise-roles (it was #7 when I wrote this, but the list may have
changed).
option
Another chance to get on the mailing list. Just hit return if you already signed
up.
option
1)
2)
3)
4)
5)
6)
choose
Our first option! Weve always used WEBrick since it is the easiest to use for
development. Choose WEBrick to keep things familiar.
option
1)
2)
3)
4)
5)
6)
choose
We could get fancy for deployment (for example, Heroku recommends Unicorn). Choose Same as development to stay in our comfort zone.
option
1)
2)
3)
choose
362
We havent explored applications that use databases in this book, but Devise
and role-based authorization require saving a User model to a database. Choose
SQLite, which is built-in and ready to run in the Mac or Ubuntu environments. Developers prefer PostgreSQL for production applications, but it takes
extra effort to set up, so well stick with SQLite for now.
option
1)
2)
3)
choose
Template engine?
ERB
Haml
Slim
Enter your selection:
In this book, all our view templates were written using the ERB template language. In the Concepts chapter in Book One, you learned that components
of Rails can be mixed for different stacks. Some developers substitute Haml or
Slim for ERB. Ive written an article on Haml and Rails if youd like to know
more. Choose ERB for now.
option
1)
2)
choose
Test framework?
None
RSpec with Capybara
Enter your selection:
Front-end framework?
None
Bootstrap 3.3
Bootstrap 2.3
Zurb Foundation 5.0
Zurb Foundation 4.0
Simple CSS
Enter your selection:
363
You learned to use Foundation in this book, but maybe youd like to see Bootstrap? Lets try it out. Choose Bootstrap 3.3.
option
1)
2)
3)
4)
5)
choose
Devise will need to send email for its forgot password feature. Configuring
email took some time for our tutorial application. Rails Composer will instantly
set up everything we need to send email using our choice of services. Choose
Gmail for now.
option
1)
2)
3)
choose
Devise modules?
Devise with default modules
Devise with Confirmable module
Devise with Confirmable and Invitable modules
Enter your selection:
Choose Devise with default modules. Devise has options, like a Confirmable
module that requires users to click a link in an email message to confirm a new
account. The Invitable module provides a feature that allows administrators
or other users to invite users to establish accounts. We wont need these extra
features.
option
1)
2)
choose
364
option
1)
2)
choose
In this book, we used the SimpleForm gem to make it easy to build forms. Lets
add it to the starter application by selecting SimpleForm.
option
1)
2)
3)
choose
You can enter your Segment.io API key here, if you know it. Otherwise, hit
return and youll get a placeholder you can replace later.
option
1)
2)
3)
choose
This option sets up your starter application for deployment to Heroku. Choose
no for now.
option
TRY IT OUT
365
In the Deploy chapter you learned that you can leave your website out of
Google search results with the robots.txt file. Lets answer y or yes and
play it safe.
option
Rails Composer will create a GitHub repository for your starter application if
your credentials are set up correctly. Lets play it safe and answer n or no
to skip the repository option.
option
Weve seen this option before. Weve been using RVM in this book, so lets answer y or yes and have Rails Composer create a rails-devise-roles gemset.
Rails Composer has all the answers it needs. On my computer, with a fast
Internet connection in the heart of San Francisco, Rails composer takes about
thirty seconds to build the starter application. It installs every needed gem;
sets configuration files; and generates views, models, controllers, and routes.
The developers who maintain the Rails Composer project have worked out any
tricky integration issues so you can expect the starter application to work without any problems.
Try It Out
Youve added a new application to your collection of projects:
$ ls -1
foobar-kadigan
learn-rails
rails-devise-roles
366
Lets examine the application.
$ cd rails-devise-roles
$ git log --oneline
277ff62 rails_apps_composer:
ee761e0 rails_apps_composer:
7a9f39c rails_apps_composer:
79cc5c9 rails_apps_composer:
35e16cf rails_apps_composer:
5327024 rails_apps_composer:
f576801 rails_apps_composer:
80fdad5 rails_apps_composer:
e416dd5 rails_apps_composer:
e2d4d58 rails_apps_composer:
7fa18b9 rails_apps_composer:
e56ace2 rails_apps_composer:
a538589 rails_apps_composer:
6f2d741 rails_apps_composer:
extras
navigation links
set up database
add README files
add analytics
add pages
front-end framework
add roles to a User model
devise
set email accounts
generators
create database
Gemfile
initial commit
When you move into the rails-devise-roles/ folder, RVM will automatically
begin using the gemset named rails-devise-roles because of the hidden .rubygemset and .ruby-version files.
Rails Composer set up a Git repository and committed files as it built the application. We can see a list of Git commits with the git log -oneline
command.
Lets try running the application:
$ rails server
=> Booting WEBrick
.
.
.
TRY IT OUT
367
368
everything in the RailsApps example applications and youll gain a solid understanding of the basic components used in real-world Rails projects. Secondly,
start building custom applications based on the Rails Composer starter applications. By starting with Rails Composer, youll skip the frustrating preliminaries
of setting up a front-end framework, authentication, or authorization and jump
right into implementing your ideas for new features. Rails Composer is often
used at hackathons, where teams race to build interesting applications for a
prize, to avoid the time sink of setting up a basic application.
A final word: Use Rails Composer judiciously. Its intended to be a tool for experienced developers who already know how to build starter applications from
scratch using databases, front-end frameworks, authentication, or authorization, and all the bells and whistles offered in the Rails Composer options. Use
it to pinpoint what you need to learn, or use it to turbocharge your learning
process, but dont use it as a crutch to avoid learning the basics. To learn Rails,
you must be able to build every starter application from scratch, without Rails
Composer.
To learn more about Rails Composer, see the Rails Composer home page and
the README for the Rails Composer project on GitHub.
Chapter 26
Version Notes
If you are reading the online edition of the book, you have the most recent
version of the book. If youve gotten your copy of the book elsewhere, you
may have an older version that doesnt have the newest updates.
Youll find the version number and release date on the first page of this book
(under the book title). Check the learn-rails GitHub repository to find out if
you have the newest version of the book. The README page on the GitHub
repo always shows the most recent version number for the book and the tutorial
application.
If you have trouble building the application in this book, and suspect something
may be out of date, you can check the Gemfile in the repo to see if weve
changed gems or specified version numbers to avoid compatibility issues. You
can also check the CHANGELOG, look at recent commits, and check the issues
to see the current state of the application.
Here are the changes Ive made.
Version 3.1.0
Version 3.1.0 was released March 1, 2016
369
370
Version 3.0.2
Version 3.0.2 was released January 30, 2016
Version 3.0.1
Version 3.0.1 was released January 29, 2016
Specify version 5.5 of the foundation-rails gem. Foundation 6 is out but Zurb
has not yet released documentation for migration from Foundation 5 to 6.
Version 3.0.0
Version 3.0.0 was released January 14, 2016
Extensive revision throughout the book, and the length of the book increased,
so the book is now two books. Book One contains the introductory and selfhelp chapters and can be read without access to a computer. Book Two contains
the step-by-step tutorial and requires use of a computer.
Switch to using the Mandrill service to send email. Previously used Gmail but
Google has taken steps to make Gmail more secure and now it can be difficult
to send email from a Rails application using Gmail.
Sending mail now requires the method deliver_now instead of deliver.
The UserMailer class now inherits from ApplicationMailer.
VERSION 2.2.2
371
Version 2.2.2
Version 2.2.2 was released October 30, 2015
Version 2.2.1
Version 2.2.1 was released September 19, 2015
Updated references to Ruby from version 2.2.0 to 2.2.3.
Updated references to Rails from 4.2.0 to Rails 4.2.4.
Updated Visitor model subscribe method for the new Gibbon 2.0 API.
Recommending Cloud9 instead of Nitrous.io because Nitrous.io is no longer
free.
Version 2.2.0
Version 2.2.0 was released June 6, 2015
For Amazon customers, added an offer to access the online version or download
a PDF at learn-rails.com.
Google now requires use of OAuth 2.0 for application access to Google Drive.
The implementation is considerably more complex than the previous imple-
372
mentation using a Gmail address and password. Ive dropped the Spreadsheet
Connection chapter.
Minor clarification in the Layout and Views chapter.
Version 2.1.6
Version 2.1.6 was released March 17, 2015
Remove references to the Thin web server in the Deploy chapter.
Correct version number for gem sass-rails in various Gemfile listings.
Fixes issue 49 and an error Sass::SyntaxError - Invalid CSS when the Foundation front-end framework is used.
In the Testing chapter, the file test/integration/home_page_test.rb was missing require test_helper.
Updated Rails Composer chapter to describe new options.
Minor improvements and corrections of typos.
Version 2.1.5
Version 2.1.5 was released March 4, 2015
Use the Ruby 1.9 hash syntax in the validates_format_of :email statement.
Minor improvements and corrections of typos.
Version 2.1.4
Version 2.1.4 was released January 3, 2015
VERSION 2.1.3
373
Version 2.1.3
Version 2.1.3 was released December 25, 2014
Updated references to Rails 4.1.8 to Rails 4.2.0.
Version 2.1.2
Version 2.1.2 was released December 4, 2014
Released for sale as a Kindle book on Amazon, with new cover art (same cat,
though).
RailsApps Tutorials now named the Capstone Rails Tutorials.
Updated references to Ruby from version 2.1.3 to 2.1.5.
Updated references to Rails 4.1.6 to Rails 4.1.8 (minor releases with bug and
security fixes).
Removed link to the (now defunct?) Lowdown web application in the Plan
Your Product chapter.
Changes to the Asynchronous Mailing section of Send Mail chapter to describe Active Job in Rails 4.2.
Minor improvements to the Dynamic Home Page, Deploy, Configure,
Troubleshoot, and Create the Application chapters.
374
Version 2.1.1
Version 2.1.1 was released October 22, 2014
Minor rewriting for clarity.
Updated Precompile Assets section of the Deploy chapter.
Mentioned explainshell.com in the Get Started chapter.
Mentioned Zeal as a Linux alternative to Dash.
Recommended book Practicing Rails by Justin Weiss.
Version 2.1.0
Version 2.1.0 was released October 12, 2014
Updated references to Ruby from version 2.1.1 to 2.1.3.
Updated references to Rails 4.1.1 to Rails 4.1.6 (minor releases with bug and
security fixes).
Four new chapters:
Testing
Rails Composer
Crossing the Chasm
Level Up
Use ActiveModel instead of the activerecord-tableless gem.
In the Configuration chapter, add a note to use spaces (not tabs) in the config/secrets.yml file.
VERSION 2.0.2
375
Updated Gems chapter to add a troubleshooting note to the Install the Gems
section (about errors with the Nokogiri gem).
Added a section on Multiple Terminal Windows to the Create the Application chapter.
In the Get Help When You Need It chapter, updated the list of recommended
newsletters, replaced rubypair.com with codermatch.me, and added a section
on code review. Removed reference to defunct Rails Development Directory.
Version 2.0.2
Version 2.0.2 was released May 6, 2014
Updated references to Rails 4.1.0 to Rails 4.1.1 (a minor release with a security
fix).
For Nitrous.io users, clarify that http://localhost:3000/ means the Preview
browser window.
Update Gems chapter, section Where Do Gems Live? to add more explanation.
Minor change to code in the Mailing List chapter, setting mailchimp_api_key
explicitly when instantiating Gibbon, for easier troubleshooting.
Version 2.0.1
Version 2.0.1 was released April 16, 2014
Minor updates for Rails 4.1.0. Mostly small changes to the Configure and
Front-End Framework chapters.
Added an explanation that, in the config/secrets.yml file, domain_name doesnt
have to be kept secret and set as a Unix environment variable.
376
Added a hint about passwords that use punctuation marks (plus a completely
irrelevant note about profanitype).
Version 2.0.0
Version 2.0.0 was released April 8, 2014
Updated references to Ruby from version 2.1.0 to 2.1.1.
Updated the book to Rails 4.1. The application name is no longer used in the
config/routes.rb file.
Rails 4.1 changes the app/assets/stylesheets/application.css.scss file. Updated the Front-End Framework chapter. Also expanded the explanation of
the Foundation grid.
In Rails 4.1, configuration variables are set in the config/secrets.yml file. The
Figaro gem is dropped, along with the config/application.yml file. Updated
the Configure chapter and references to configuration variables throughout
the book.
VERSION 1.19
377
Version 1.19
Version 1.19 was released February 1, 2014
Updated the book to use Foundation 5.0. Foundation 5.0.3 was released January 15, 2014 (earlier versions 5.0.1 and 5.0.2 were incompatible with Rails
Turbolinks and the Rails asset pipeline). Changed the Gemfile to remove
gem compass-rails and replace gem zurb-foundation with gem
foundation-rails. Updated a line in the Front-End Framework chapter for Foundation 5.0:
The files navigation.html.erb and application.html.erb are changed for Foundation 5.0. The Bootstrap front-end framework is now independent of Twitter, so I call it Bootstrap not Twitter Bootstrap. Revised the chapter Just
Enough Ruby to incorporate suggestions from technical editor Pat Shaughnessy. Revised the chapter Request and Response to incorporate suggestions
from technical editor Kirsten Jones. Minor rewriting for clarity throughout.
378
Version 1.18
Version 1.18 was released January 10, 2014
Updated references to Ruby from version 2.0.0 to 2.1.0. Changed one line in
the Front-End Framework chapter to accommodate a change in the rails_layout
gem version 1.0.1. The command was:
$ rails generate layout foundation4 --force
Changed to:
$ rails generate layout:install foundation4 --force
Version 1.17
Version 1.17 was released December 21, 2013
Updated Rails version from 4.0.1 to 4.0.2 .
Changed Gemfile to remove gem compass-rails, > 2.0.alpha.0
and replace it with gem compass-rails, > 1.1.2. The 2.0.alpha.0
version was yanked from the RubyGems server. The compass-rails gem is
needed for Foundation 4.3. It will not be needed for Foundation 5.0.
VERSION 1.17
379
380
Chapter 27
Credits
The book was created with the encouragement, financial support, and editorial
assistance of hundreds of people in the Rails community.
Daniel Kehoe wrote the book and implemented the application.
381
382
Financial Backers
The following individuals provided financial contributions of over $50 to help
launch the book. Please join me in thanking them for their encouragement and
support.
Al Zimmerman, Alan W. Smith, Alberto A. Coln Viera, Andrew Terry, Avi
Flombaum, Brian Hays, Charles Treece, Dave Doolin, Denzil Villarico, Derek
Rockwell, Eito Katagiri, Evan Sparkman, Frank Castle, Fred Dixon, Fred Schoeneman, Gant Laborde, Gardner Monks, Gerard de Brieder, GoodWorksOnEarth.org,
Hanspeter Leupin, Harald Lazardzig, Harsh Patel, James Bond, Jared Koumentis, Jason Landry, Jeff Whitmire, Jesse House, Joe Wilmoth Jr., John Shannon,
Joost Baaij, Juan Cristobal Pazos, Kathleen Sidenblad, Laird Hayward, Logan Hasson, Ludovic Kuty, Mark Gilbert, Matt Esterly, Mike Gilbert, Niko
Roberts, Norman Cohen, Paul Philippov, Robert Nadar, Rogier Hof, Ross Kinney, Ruben Calzadilla, Stephane Moreau, Susan Wilson, Sven Fuchs, Thomas
Nitsche, Tom Michel, Youn Shin Kang, Yuen Lock
Technical Editors
Rails and Ruby experts are very busy. I am very grateful for the assistance I
received from my colleagues for the technical review of individual chapters.
Kirsten Jones, reviewed the chapter Request and Response
Pat Shaughnessy, author of Ruby Under a Microscope, reviewed the
chapter Just Enough Ruby
Noel Rappin, author of Rails Test Prescriptions, reviewed chapters 1-7,
and the Testing chapter
Aaron Sumner, author of Everyday Rails Testing with RSpec, reviewed
the Testing chapter
Ken Collins reviewed the Testing chapter
CREDITS
383
Photos
Images provided by the lorempixel.com service are used under the Creative
Commons license. Visit the Flickr accounts of the photographers to learn more
about their work:
photo of a white cat by Tomi Tapio
photo of a cat by Steve Garner
384
Comments
I regularly update the book. Your comments and suggestions for improvements
are welcome.
Feel free to email me directly at [email protected].
Are you stuck with code that wont work? Stack Overflow provides a questionand-answer forum for readers of this book. Use the tag learn-ruby-on-rails
or railsapps when you post your question.
Found a bug in the tutorial application? Please create an issue on GitHub.