Rails is a Ruby-based MVC web framework created in 2003 by @dhh and is widely used.
Twitter, Disney, Hulu, Yellow Pages, GroupOn, GitHub, LivingSocial, IndieGoGo...
Rails, like Sinatra, is a DSL for creating web pages.
Fully open source, it is constantly adapting to new trends and requirements of modern web development. It's distributed as a family of gems; there were 28 individual gems last time I counted that made up the core of Rails.. The philosophy of Rails (ostensibly) boils down to two things:
- DRY - Don't Repeat Yourself (yawn)
- Convention over Configuration (a milliont tiny hearts, all aflutter)
$ gem install rails --no-rdoc --no-ri
$ rails new rails-blog -T
Questions: Why --no-rdoc
and --no-ri
? Why -T
?
The docs take a year to install, and we'll never look at them
-T
excludes the default test framework (always use this, even if using rspec)
The Rails gem includes a command-line tool rails
that has a number of options. With it we can start a server, quickly create new classes, work with the database, and many other things. The command new
creates a new directory and creates all the subdirectories and files a Rails application needs to have in order to get started.
You can see all of the command line options that the Rails application builder accepts by running $ rails new -h
.
Let's take a tour: $ cd rails-blog/
Here's a table I shamelessly stole from the Rails Getting Started Guide:
File/Folder | Purpose |
---|---|
app/ | Contains the controllers, models, views, helpers, mailers and assets for your application. You'll focus on this folder for the remainder of this guide. |
bin/ | Contains the rails script that starts your app and can contain other scripts you use to setup, deploy or run your application. |
config/ | Configure your application's routes, database, and more. This is covered in more detail in Configuring Rails Applications. |
config.ru | Rack configuration for Rack based servers used to start the application. |
db/ | Contains your current database schema, as well as the database migrations. |
Gemfile & Gemfile.lock | These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see the Bundler website. |
lib/ | Extended modules for your application. |
log/ | Application log files. |
public/ | The only folder seen by the world as-is. Contains static files and compiled assets. |
Rakefile | This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile, you should add your own tasks by adding files to the lib/tasks directory of your application. |
README.rdoc | This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on. |
test/ | Unit tests, fixtures, and other test infrastructure. |
tmp/ | Temporary files (like cache, pid, and session files). |
vendor/ | A place for all third-party code. In a typical Rails application this includes vendored gems. |
$ rails server
This command starts the default web server WEBrick
which we can access at http://localhost:3000 on our computer. It will display the output of the log, showing all the errors, warnings, and notifications generated by the app, all the DB queries, and some basic timing information. Let's take a look at that!