Home > Articles > Programming > Ruby

Ruby on Rails 3 Tutorial: From Zero to Deploy

  • Print
  • + Share This
Michael Hartl gets you started with Ruby on Rails by installing all the necessary software and setting up your development environment He'll then show you how to create your first Rails application, called (appropriately enough) first_app.
This chapter is from the book

Welcome to Ruby on Rails™ 3 Tutorial: Learn Rails by Example. 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 Ruby on Rails Tutorial, you will have all the knowledge you need to develop and deploy your own custom web applications. 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 Ruby on Rails Tutorial uses Rails 3.0, the knowledge you gain here will be fully up to date with the latest and greatest version of Rails.1


In this first chapter, we'll get started with Ruby on Rails by installing all the necessary software and setting up our development environment (Section 1.2). We'll then create our first Rails application, called (appropriately enough) first_app. Rails Tutorial emphasizes 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 will be 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.1) to generate code; since this code is both ugly and complex, Chapter 2 will focus on interacting with the demo app through its URLs 3 using a web browser.

In Chapter 3, we'll create a sample application (called sample_app), this time 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 10, 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 11 and Chapter 12 we'll add microblogging and social features to make a working example site.


1.1 Introduction



Rails also adapts rapidly to new developments in web technology and framework design. For example, Rails was one of the first frameworks to fully digest and implement the REST architectural style for structuring web applications (which we'll be learning about throughout this tutorial). And when other frameworks develop successful new techniques, Rails creator David Heinemeier Hansson and the Rails core team don't hesitate to incorporate their ideas. Perhaps the most dramatic example is the merger of Rails and Merb, a rival Ruby web framework, so that Rails now benefits from Merb's modular design, stable API, and improved performance. (Anyone who has attended a talk by Merb developer and Rails core team member Yehuda Katz can't help but notice what an extremely good idea it was to bring the Merb team on board.)


1.1.1 Comments for Various Readers


Rails derives much of its power from "magic"—that is, framework features (such as automatically inferring object attributes from database columns) that accomplish miracles but whose mechanisms can be rather mysterious. Ruby on Rails Tutorial is not designed to explain this magic—mainly because most Rails application developers never need to know what's behind the curtain. (After all, Ruby itself is mostly written in the C programming language, but you don't have to dig into the C source to use Ruby.) If you're a confirmed pull-back-the-curtain kind of person, I recommend The Rails 3 Way by Obie Fernandez as a companion volume to Ruby on Rails Tutorial.






After finishing Ruby on Rails Tutorial, I recommend that newer programmers read Beginning Ruby by Peter Cooper, which shares the same basic instructional philosophy as Rails Tutorial. I also recommend The Ruby Way by Hal Fulton. Finally, to gain a deeper understanding of Rails, I recommend The Rails 3 Way by Obie Fernandez.

Web applications, even relatively simple ones, are by their nature fairly complex. If you are completely new to web programming and find Rails Tutorial overwhelming, it could be that you're not quite ready to make web applications yet. In that case, I'd suggest learning the basics of HTML and CSS and then giving Rails Tutorial another go. (Unfortunately, I don't have a personal recommendation here, but Head First HTML looks promising, and one reader recommends CSS: The Missing Manual by David Sawyer McFarland.) You might also consider reading the first few chapters of Beginning Ruby, which starts with sample applications much smaller than a full-blown web app.

Experienced programmers new to web development: Your previous experience means you probably already understand ideas like classes, methods, data structures, etc., which is a big advantage. Be warned that if your background is in C/C++ or Java, you may find Ruby a bit of an odd duck, and it might take time to get used to it; just stick with it and eventually you'll be fine. (Ruby even lets you put semicolons at the ends of lines if you miss them too much.) Rails Tutorial covers all the web-specific ideas you'll need, so don't worry if you don't currently know a PUT from a POST.


Experienced Ruby programmers: The set of Ruby programmers who don't know Rails is a small one nowadays, but if you are a member of this elite group you can fly through this book and then move on to The Rails 3 Way by Obie Fernandez.

Inexperienced Rails programmers: You've perhaps read some other tutorials and made a few small Rails apps yourself. Based on reader feedback, I'm confident that you can still get a lot out of this book. Among other things, the techniques here may be more up to date than the ones you picked up when you originally learned Rails.




  • Railscasts: Excellent free Rails screencasts.
  • PeepCode, Pragmatic.tv, EnvyCasts: Excellent commercial screencasters.

1.1.2 "Scaling" Rails

Before moving on with the rest of the introduction, I'd like to take a moment to address the one issue that dogged the Rails framework the most in its early days: the supposed inability of Rails to "scale"—i.e., to handle large amounts of traffic. Part of this issue relied on a misconception; you scale a site, not a framework, and Rails, as awesome as it is, is only a framework. So the real question should have been, "Can a site built with Rails scale?" In any case, the question has now been definitively answered in the affirmative: some of the most heavily trafficked sites in the world use Rails. Actually doing the scaling is beyond the scope of just Rails, but rest assured that if your application ever needs to handle the load of Hulu or the Yellow Pages, Rails won't stop you from taking over the world.

1.1.3 Conventions in This Book

The conventions in this book are mostly self-explanatory; in this section, I'll mention some that may not be. First, both the HTML and PDF editions of this book are full of links, both to internal sections (such as Section 1.2) and to external sites (such as the main Ruby on Rails download page).5

Second, your humble author is a Linux/OS X kind of guy, and hasn't used Windows as his primary OS for more than a decade; as a result, Rails Tutorial has an unmistakable Unix flavor.6 For example, in this book all command line examples use a Unix-style command line prompt (a dollar sign):

$ echo "hello, world"
hello, world

Rails comes with lots of commands that can be run at the command line. For example, in Section 1.2.5 we'll run a local development web server as follows:

$ rails server





This means that when referring to the file


I'll omit the Rails root and write config/routes.rb for brevity.

Finally, Rails Tutorial often shows output from various programs (shell commands, version control status, Ruby programs, etc.). Because of the innumerable small differences between different computer systems, the output you see may not always agree exactly with what is shown in the text, but this is not cause for concern. In addition, some commands may produce errors depending on your system; rather than attempt the Sisyphean task of documenting all such errors in this tutorial, I will delegate to the "Google the error message" algorithm, which among other things is good practice for real-life software development.

  • + Share This
  • 🔖 Save To Your Account