Chapter 1. Zero to Sixty: Introducing Rails

When we wrote the first version of this book, Rails was just starting to ramp up as a framework. Now, the exploding web development platform is working its way into the mainstream. Companies like Twitter have bet on Rails and won big, and others have tried Rails and crashed hard. You should be comfortable that you’re entering this ecosystem not as a pioneer, but as part of a much greater wave that’s sweeping through the whole computing profession. If you read the first edition, you’ll notice that we’re not selling the framework quite so hard. In all honesty, we don’t have to. Let’s lay the basic foundation so we can get to work.

Rails is a framework for building database-backed web applications. Based on the Ruby programming language, Rails is best for applications that need to be developed quickly without sacrificing a clean structure that can also be well-maintained. Ruby is interpreted, but it’s fast enough to attack websites with all but the most demanding performance characteristics. And it’s tremendously popular. That popularity means you can find what you need to build projects in Rails.

Since the first edition of this book was released, the Rails core team released milestone versions 1.1, 1.2, 2.0, and now 2.1. Today, you can find excellent hosting from dozens of vendors depending on how much management you’re willing to do. And technical officers like me (Bruce Tate) are bringing Rails into the enterprise to run our core businesses. The Rails framework has since led to rapid investment in the Ruby programming language, too, including evolving implementations on the Java Virtual Machine, the Microsoft Common Language Runtime, and two emerging Ruby virtual machines. Rails is also redefining web development frameworks in other languages as well. Java, PHP, Python, Lisp, Groovy, and Perl each have more than one Rails-like framework. The Rails convention-based approach is overtaking standard configuration-based approaches just about everywhere.

In this book, we will walk you through everything you need to get a simple application up and running. We promise to move slowly enough that you can absorb the concepts, but quickly enough so that you will have a working application after following along for a few hours. Let’s get started.

Putting Rails into Action

We’re going to blow through installation quickly because installation details can change with Rails itself and the individual platforms that support it. The best place to go, regardless of your platform, is the Rails wiki at http://wiki.rubyonrails.org/rails/pages/GettingStartedWithRails. We’ll give you a condensed version here: you’ll install Ruby, Rails, and then your database. First, install Ruby. If you’re running OS X, chances are good that Ruby is already there, but older versions will need an upgrade. If you’re running Windows, there are some good one-click installers that you can use such as the Ruby one-click installer at http://rubyinstaller.rubyforge.org. For now, just install Ruby, and we’ll walk you through the rest.

Once you’ve installed Ruby, you could manually install all of the components for Rails, but Ruby has a packaging and deployment feature named gems. The gem installer accesses a central repository and downloads an application unit, called a gem, and all its dependencies. If you haven’t done so, install Rails with this command:[1]

sudo gem install rails --include-dependencies  -v 2.1

The command for installing Ruby components on all platforms is the same: gem install. The permissions required will vary from platform to platform. On OS X, sudo elevates your permissions for this command to superuser status. You’ll want to omit the sudo portion of this command on Windows. If things go well, you’ll see a list of a bunch of different gems fly by. These are Rails and all of the dependencies that Rails requires. That’s it—Rails is installed. There’s one caveat: you also need to install the database support for your given database. In this book, we’re going to use SQLite, but you can use any database engine supported by Rails. On Unix, chances are SQLite is already installed. On Windows, you’ll need to pull down the SQLite DLL and command-line client. For more details, go to http://wiki.rubyonrails.com/rails/pages/HowtoUseSQLite. With Ruby, Rails, and a database, you’re ready to create a project:

>rails chapter-1
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
      create  components
      create  db
      create  doc
      create  lib

...
      create  test/functional
      create  test/integration
      create  test/unit
      create  vendor
...
      create  app/controllers/application.rb
      create  app/helpers/application_helper.rb
      create  test/test_helper.rb
      create  config/database.yml
...

We truncated the list, but you get the picture. Rails creates folders that you’ll use to hold the model, view, and controller portions of your application code, different kinds of test cases, database configuration, and third-party libraries that you might choose to install later. All Rails projects will use this same organization with a few slight variations. You’ll run just about all of your Rails scripts from your new project directory. Go to your project directory with cd chapter-1. In the next section, we’ll take a look at the organization of your project in more detail.



[1] If you want to code along with us, make sure you’ve installed Ruby and gems. Appendix A contains detailed installation instructions.

Get Rails: Up and Running, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.