Rails Introduction

Kung-Fu

“Ruby on Rails is astounding. Using it is like watching a kung-fu movie, where a dozen bad-ass frameworks prepare to beat up the little newcomer only to be handed their asses in a variety of imaginative ways.”

-Nathan Torkington, O’Reilly Program Chair for OSCON

Installation

Rails Background

Rails Elevator Pitch

Ruby on Rails is an open-source web framework that’s optimized for programmer happiness and sustainable productivity. It let’s you write beautiful code by favoring convention over configuration.

Rails Philosophy and Strengths

MVC Request Cycle

Building a Demo Application

  • Generating an application with the rails command
  • Hello world
  • Rails actions and templates
  • The MVC request cycle and routing: :controller/:action/:id
  • Database configuration in config/database.yml
  • Code generation with script/generate scaffold
  • Migrations
  • Naming conventions
  • The Rails command line – script/console
  • Rails environments
  • Configuration
  • The error log

Directory Structure

app
      controllers
      helpers
      models
      views
        layouts
config
      environment.rb
      routes.rb
db
      database.yml
      migrations
lib
log
public  
script
test
vendor
  plugins
  gems
  rails

MVC Request Cycle in Detail

  1. A request is made from the browser to the URL http://localhost:3000/users/show/1.
  2. A Rails server running locally on port 3000 receives the request and the dispatcher is invoked. Rails tries to find a route (in the file config/routes.rb) to match the URI /users/show/1. The default route ’:controller/:action/:id’ matches the URI and the dispatcher instantiates the users controller and invokes its show method with the :id parameter set to 1.
  3. The show method in the users controller fetches the user model object from the database with id equal to 1.
  4. The show method renders its view (the file show.rhtml) which generates an HTML page with the user info from the user model object.
  5. Rails sends the generated HTML from the view back to the browser.

Rake

Useful Rake Tasks

script/*

Environments

config/environment.rb and config/initializers

The Rails Configuration Object


# Defined in railties/lib/initializer.rb
Rails.root
Rails.env
Rails.version
Rails.configuration.load_paths
Rails.configuration.gems
Rails.configuration.plugins
Rails.configuration.time_zone
Rails.configuration.i18n

Debugging