Welcome to the Ruby on Rails Tutorial. The goal of this book is to be the best answer to the question, “If I want to learn web development with Ruby on Rails, where should I start?” By the time you finish the Ruby on Rails Tutorial, you will have all the skills you need to develop and deploy your own custom web applications with Rails. You will also be ready to benefit from the many more advanced books, blogs, and screencasts that are part of the thriving Rails educational ecosystem. Finally, since the Ruby on Rails Tutorial uses Rails 4, the knowledge you gain here represents the state of the art in web development. (The most up-to-date version of the Ruby on Rails Tutorial can be found on the book’s website athttp://railstutorial.org/; if you are reading this book offline, be sure to check the online version of the Rails Tutorial book at http://railstutorial.org/book for the latest updates.)
(Note: The present volume is the Rails 4.0 version of the book, which means that it has been revised to be compatible with Rails 4.0, but it is not yet a new edition because the changes in Rails don’t yet justify it. From the perspective of an introductory tutorial, the differences between Rails 4.0 and the previous version, Rails 3.2, are slight. Indeed, although there are a large number of miscellaneous small changes (Box 1.1), for our purposes there is only one significant difference, a new security technique called strong parameters, covered inSection 7.3.2. Once the changes in Rails justify the effort, I plan to prepare a full new edition of the Rails Tutorial, including coverage of topics such as Turbolinks and Russian doll caching, as well as some new aspects of RSpec, such as feature specs.)
Box 1.1. Diffs from the 2nd edition
This is a (nearly) comprehensive list of differences between the 2nd edition of the Ruby on Rails Tutorial and the present version. (The only really important one is the change to strong parameters; the others are all relatively minor.) This list is presented for the convenience of those who read the 2nd edition (or are otherwise familiar with Rails 3.2) and want a summary of the diffs. If you don’t already have experience with Rails 3.2, you should probably ignore this list.
In what follows, each item includes a reference to a section or code listing with an example of the change in question.
- Change Rails 3.2 to Rails 4.0 (Section 1.2.2)
- Explicitly include Capybara DSL (Listing 3.10)
- Change RSpec .should to expect().to (Section 3.2.1)
- Change have_selector(’title’, …) to have_title(…) (Section 3.3.1)
- Change HTTP verb from PUT to PATCH for updates (Box 3.3)
- Change root to: ’path’ to root ’path’ (Listing 5.26)
- Change find_by_thing(…) to find_by(thing: …) (Section 6.1.4)
- Switch from rake db:test:prepare to rake test:prepare (Section 6.2.1)
- Change from attr_accessible to strong parameters (Section 7.3.2)
- Change to hashed remember tokens (Section 8.2.1)
- Change before_filter to before_action (Listing 9.12)
- Use Capybara’s match: :first to click on the first matching link (Listing 9.42)
- Change default_scope from a hash argument to a lambda (Listing 10.11)
- Change dup to to_a (Listing 10.12)
- Use XPath to test button toggling (Section 11.2.4)
It’s worth emphasizing that the goal of this book is not merely to teach Rails, but rather to teach web development with Rails, which means acquiring (or expanding) the skills needed to develop software for the World Wide Web. In addition to Ruby on Rails, this skillset includes HTML & CSS, databases, version control, testing, and deployment. To accomplish this goal, the Ruby on Rails Tutorial takes an integrated approach: you will learn Rails by example by building a substantial sample application from scratch. As Derek Sivers notes in the foreword, this book is structured as a linear narrative, designed to be read from start to finish. If you are used to skipping around in technical books, taking this linear approach might require some adjustment, but I suggest giving it a try. You can think of the Ruby on Rails Tutorial as a video game where you are the main character, and where you level up as a Rails developer in each chapter. (The exercises are the minibosses.)
In this first chapter, we’ll get started with Ruby on Rails by installing all the necessary software and by setting up our development environment (Section 1.2). We’ll then create our first Rails application, called (appropriately enough) first_app. The Rails Tutorialemphasizes good software development practices, so immediately after creating our fresh new Rails project we’ll put it under version control with Git (Section 1.3). And, believe it or not, in this chapter we’ll even put our first app on the wider web by deploying it to production (Section 1.4).
In Chapter 2, we’ll make a second project, whose purpose is to demonstrate the basic workings of a Rails application. To get up and running quickly, we’ll build this demo app(called demo_app) using scaffolding (Box 1.2) to generate code; since this code is both ugly and complex, Chapter 2 will focus on interacting with the demo app through its URIs (often called URLs)1 using a web browser.
The rest of the tutorial focuses on developing a single large sample application (calledsample_app), writing all the code from scratch. We’ll develop the sample app using test-driven development (TDD), getting started in Chapter 3 by creating static pages and then adding a little dynamic content. We’ll take a quick detour in Chapter 4 to learn a little about the Ruby language underlying Rails. Then, in Chapter 5 through Chapter 9, we’ll complete the foundation for the sample application by making a site layout, a user data model, and a full registration and authentication system. Finally, in Chapter 10 and Chapter 11 we’ll add microblogging and social features to make a working example site.
The final sample application will bear more than a passing resemblance to a certain popularsocial microblogging site—a site which, coincidentally, was also originally written in Rails.Though of necessity our efforts will focus on this specific sample application, the emphasis throughout the Rails Tutorial will be on general principles, so that you will have a solid foundation no matter what kinds of web applications you want to build.
Box 1.2. Scaffolding: Quicker, easier, more seductive
From the beginning, Rails has benefited from a palpable sense of excitement, starting with the famous 15-minute weblog video by Rails creator David Heinemeier Hansson. That video and its successors are a great way to get a taste of Rails’ power, and I recommend watching them. But be warned: they accomplish their amazing fifteen-minute feat using a feature called scaffolding, which relies heavily on generated code, magically created by the Railsgenerate command.
When writing a Ruby on Rails tutorial, it is tempting to rely on the scaffolding approach—it’squicker, easier, more seductive. But the complexity and sheer amount of code in the scaffolding can be utterly overwhelming to a beginning Rails developer; you may be able to use it, but you probably won’t understand it. Following the scaffolding approach risks turning you into a virtuoso script generator with little (and brittle) actual knowledge of Rails.
In the Ruby on Rails Tutorial, we’ll take the (nearly) polar opposite approach: althoughChapter 2 will develop a small demo app using scaffolding, the core of the Rails Tutorial is the sample app, which we’ll start writing in Chapter 3. At each stage of developing the sample application, we will write small, bite-sized pieces of code—simple enough to understand, yet novel enough to be challenging. The cumulative effect will be a deeper, more flexible knowledge of Rails, giving you a good background for writing nearly any type of web application.