Foreword
Ruby on Rails turned web development upside down. By abstracting the core of web programming in an unparalleled way, this unique piece of technology changed the game forever. With Rails, you can write web applications quickly without compromising quality. You can be very productive, write little code, deal with almost no configuration, and adapt to changes in specifications with agility, all while keeping a well-organized and elegant code base.
With Ruby on Rails, you feel empowered. Want to explore something with a quick prototype? Delivered in no time. Need to develop a solid production-ready website? Presto!
A decade later, the fundamental principles underlying the Rails breakthrough still permeate and drive the design of the framework and the way Rails applications are developed. Youll learn about these fundamental aspects of the Rails culture explicitly in the second chapter of Rails Crash Course and implicitly by example throughout the book.
While the foundational ideas behind Ruby on Rails remain key, the framework has evolved. Ruby on Rails has been extended here, shrunk there, iterated, and refined. The world in which Rails applications live has also evolved. Rails Crash Course presents the most modern and idiomatic Ruby on Rails.
But first things first. Ruby on Rails is a web framework written in the Ruby programming language. Think about Rails as a huge Ruby library: A Rails application is written in Ruby and uses the classes and core support provided by Ruby on Rails. Therefore, you definitely have to know some Ruby in order to write Ruby on Rails applications! The first chapter of Rails Crash Course introduces Ruby in case you are not familiar with it. Ruby is a powerful programming language, but it is easy to learn, and with that quick introduction, youll know enough to begin. Later, more advanced Ruby is explained.
Once you know some Ruby, youre going to learn Rails. All aspects of the framework are covered, including how to write models that get persisted easily in a database, how to validate data, how to handle web requests, how to serve HTML, and so on.
Rails Crash Course covers all sides of Ruby on Rails, but then it takes you beyond the basics. For example, youll learn some advanced Active Record, authentication, and how to write an application that provides a REST API, but youll also learn about testing, security, performance, debugging, and other practical concerns of writing real-world web applications.
Further, Rails Crash Course guides you step-by-step all the way through uploading your application to production platforms and seeing it run on the Internet. Thats an amazing experience. Youll learn how to deploy to Heroku and how to deploy to a computer in the Amazon cloud. While the servers needed to run an application for learning are small and free, they are the real thing: Youll upload to the exact same services big companies are deploying their applications to. Rails Crash Course is a superb introduction to Ruby on Rails, and by reading it, youll get a solid understanding of Ruby on Rails and its ecosystem in a broad sense.
Welcome, and enjoy!
Xavier Noria
Cubelles, Spain
July 2014
Acknowledgments
First, Id like to thank everyone at No Starch Press for giving me the opportunity to write this book. Jennifer Griffith-Delgado guided me through the process of writing this book. As a first-time author, I found her corrections and suggestions invaluable. Serena Yang managed the production of the book and kept everything moving forward even as I dragged my feet at times.
Xavier Noria did an outstanding job as the technical reviewer. Not only did he make sure the code samples were correct and followed best practices, he also pointed out several places where my explanations could be better. If there are any mistakes left in this book, Im sure its only because I didnt correct them when he pointed them out.
I am grateful to Tim Taylor for introducing me to programming by teaching me BASIC on a Commodore 64 in the 7th grade. Weve come a long way since then. Id also like to thank a few more of my friends from the Paris Independent School District who convinced me that I could teach even though Im not really a teacher: Karol Ackley, Paula Alsup, Denise Kornegay, Dee Martin, and Frances Reed.
Thanks to everyone in the amazing Austin technology community. Special thanks to Austin on Rails and its founder Damon Clinkscales. Thank you to everyone who attended one of my Rails classes or conference sessions. This book grew out of the curriculum I developed for those classes. Your questions and comments helped clarify the material in this book.
Finally, my most heartfelt thanks to my family: my wife, Paige, and our sons, Matthew and Wyatt. The book is finally done; lets go play!
Introduction
The Ruby on Rails framework emphasizes developer productivity, making it possible to implement sites that would once have taken months to build in a matter of weeksor even days! Thanks to the Ruby programming language and principles such as convention over configuration and dont repeat yourself , Rails developers spend less time configuring their applications and more time writing code.
Ruby on Rails is also a full-stack web framework, meaning it handles everything from accessing data in a database to rendering web pages in the browser. As a full-stack framework, Rails is made up of a seemingly endless list of different components, such as Active Record, the asset pipeline, CoffeeScript, Sass, jQuery, turbolinks, and a variety of testing frameworks.
This book aims to cut through that list and explain exactly what you need to know to develop your own Ruby on Rails applications. After you gain some experience with the fundamentals of Rails, Ill introduce and explain new components of the framework as needed.
By the end, youll know how to build your own Rails application from scratch. Youll add tests to ensure features work as expected, protect your application and your users from security vulnerabilities, optimize your applications performance, and finally deploy your application to your own server.
Who This Book Is For
I assume you have some experience with web development before starting this book. You should be familiar with HTML and CSS. You should know what an H1
element is and how to add images and links to a web page. Some knowledge of object-oriented programming is helpful but not required.
Youll use your computers terminal (or command prompt) to enter commands, but you dont need much prior experience with terminal commands to follow the examples. In addition to the terminal, youll also need a text editor for writing Ruby code. Many Rails developers use a vintage editor, such as Vim or Emacs.
If you dont already have a preferred text editor, I recommend Sublime Text. A free trial of Sublime Text is available online at http://www.sublimetext.com/ . The free trial version never expires, but it does occasionally prompt you to purchase a license.
Overview
This book is divided into two parts. The first part covers the fundamentals of the Ruby language and the Ruby on Rails framework. The second covers advanced topics in both Ruby and Ruby on Rails. There are exercises at the end of every chapter, and solutions for them appear at the end of the book.
covers the basics of Ruby, including datatypes, control flow, methods, and classes.
covers the basics of Ruby on Rails. Topics include Rails principles, the directory structure used by Rails applications, and common Rails commands. Youll create your first Rails application at the end of this chapter!