You are previewing Beginning Rails 3.
O'Reilly logo
Beginning Rails 3

Book Description

Beginning Rails 3 is the practical starting point for anyone wanting to learn how to build dynamic web applications using the latest release of the Rails framework for Ruby. You'll learn how all of the components of Rails fit together and how you can leverage them to create sophisticated web applications with less code and more joy.

This book is particularly well suited to those with little or no experience with web application development, or who have some experience but are new to Rails. Beginning Rails 3 assumes basic familiarity with web terms and technologies, but doesn't require you to be an expert. Rather than delving into the arcane details of Rails, the focus is on the aspects of the framework that will become your pick, shovel, and axe. Part history lesson, part introduction to object-oriented programming, and part dissertation on open source software, this title doesn't just explain how to do something in Rails, it explains why.

  • Learn to create Rails web applications from scratch

  • Includes a gentle introduction to the Ruby programming language

  • Completely updated to include the new features of Rails 3

Table of Contents

  1. Copyright
  2. About the Authors
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Introduction
    1. The Beginning Rails Website
  6. 1. Introducing the Rails Framework
    1. 1.1. The Rise and Rise of the Web Application
    2. 1.2. The Web Isn't Perfect
    3. 1.3. The Good Web Framework
    4. 1.4. Enter Rails
      1. 1.4.1. Rails Is Ruby
      2. 1.4.2. Rails Encourages Agility
        1. 1.4.2.1. Less Software
        2. 1.4.2.2. Convention Over Configuration
        3. 1.4.2.3. Don't Repeat Yourself
      3. 1.4.3. Rails Is Opinionated Software
      4. 1.4.4. Rails Is Open Source
    5. 1.5. The MVC Pattern
      1. 1.5.1. The MVC Cycle
      2. 1.5.2. The Layers of MVC
        1. 1.5.2.1. Models
        2. 1.5.2.2. Controllers
        3. 1.5.2.3. Views
    6. 1.6. The Libraries That Make Up Rails
    7. 1.7. Rails Is Modular
    8. 1.8. Rails Is No Silver Bullet
    9. 1.9. Summary
  7. 2. Getting Started
    1. 2.1. An Overview of Rails Installation
    2. 2.2. Installing on Mac OS X 10.6 Snow Leopard
      1. 2.2.1. Installing the Apple Developer Tools (Xcode)
      2. 2.2.2. Updating RubyGems and Installing Rails
    3. 2.3. Installing on Windows
      1. 2.3.1. Installing Ruby
      2. 2.3.2. Installing Rails
      3. 2.3.3. Installing SQLite
    4. 2.4. Installing on Linux
      1. 2.4.1. Installing Ruby
      2. 2.4.2. Updating RubyGems
      3. 2.4.3. Installing Rails
      4. 2.4.4. Installing SQLite
    5. 2.5. Creating Your First Rails Application
      1. 2.5.1. Starting the Built-In Web Server
      2. 2.5.2. Generating a Controller
      3. 2.5.3. Creating an Action
      4. 2.5.4. Creating a Template
    6. 2.6. Summary
  8. 3. Getting Something Running
    1. 3.1. An Overview of the Project
    2. 3.2. Creating the Blog Application
      1. 3.2.1. Creating the Project Databases
      2. 3.2.2. Creating the Article Model
      3. 3.2.3. Creating a Database Table
      4. 3.2.4. Generating a Controller
      5. 3.2.5. Up and Running with Scaffolding
      6. 3.2.6. Adding More Fields
      7. 3.2.7. Adding Validations
      8. 3.2.8. Generated Files
    3. 3.3. Summary
  9. 4. Working with a Database: Active Record
    1. 4.1. Introducing Active Record: Object-Relational Mapping on Rails
      1. 4.1.1. What About SQL?
      2. 4.1.2. Active Record Conventions
    2. 4.2. Introducing the Console
    3. 4.3. Active Record Basics: CRUD
      1. 4.3.1. Creating New Records
        1. 4.3.1.1. Using the new Constructor
        2. 4.3.1.2. Using the create Method
      2. 4.3.2. Reading (Finding) Records
        1. 4.3.2.1. Finding a Single Record Using an ID
        2. 4.3.2.2. Finding a Single Record Using first
        3. 4.3.2.3. Finding All Records
        4. 4.3.2.4. Finding with Conditions
        5. 4.3.2.5. Using Dynamic Finders
      3. 4.3.3. Updating Records
      4. 4.3.4. Deleting Records
        1. 4.3.4.1. Using destroy
        2. 4.3.4.2. Using delete
        3. 4.3.4.3. Deleting with Conditions
    4. 4.4. When Good Models Go Bad
    5. 4.5. Summary
  10. 5. Advanced Active Record: Enhancing Your Models
    1. 5.1. Adding Methods
    2. 5.2. Using Associations
      1. 5.2.1. Declaring Associations
      2. 5.2.2. Creating One-to-One Associations
        1. 5.2.2.1. Adding the User and Profile Models
      3. 5.2.3. Creating One-to-Many Associations
        1. 5.2.3.1. Associating User and Article Models
        2. 5.2.3.2. Creating a New Associated Object
      4. 5.2.4. Applying Association Options
        1. 5.2.4.1. Specifying a Default Order
        2. 5.2.4.2. Specifying Dependencies
      5. 5.2.5. Creating Many-to-Many Associations
        1. 5.2.5.1. Seeding Data
      6. 5.2.6. Creating Rich Many-to-Many Associations
    3. 5.3. Advanced Finding
      1. 5.3.1. Using the where Method
      2. 5.3.2. Using a SQL Fragment
      3. 5.3.3. Using an Array Condition Syntax
      4. 5.3.4. Using Association Proxies
      5. 5.3.5. Other Finder Methods
      6. 5.3.6. Default Scope
      7. 5.3.7. Named Scope
    4. 5.4. Applying Validations
      1. 5.4.1. Using Built-in Validations
        1. 5.4.1.1. Validating That a Value Has Been Entered
        2. 5.4.1.2. Validating That a Value Is Unique
        3. 5.4.1.3. Validating Length or Size
        4. 5.4.1.4. Validating the Format of an Attribute
        5. 5.4.1.5. Validating Confirmation
        6. 5.4.1.6. Other Validations
      2. 5.4.2. Building Custom Validation Methods
    5. 5.5. Making Callbacks
    6. 5.6. Observers
      1. 5.6.1. Updating the User Model
    7. 5.7. Reviewing the Updated Models
    8. 5.8. Summary
  11. 6. Action Pack: Working with the View and the Controller
    1. 6.1. Action Pack Components
      1. 6.1.1. Action Controller
      2. 6.1.2. Action View
      3. 6.1.3. Embedded Ruby
      4. 6.1.4. Helpers
      5. 6.1.5. Routing
      6. 6.1.6. RESTful Resources
      7. 6.1.7. The Action Pack Request Cycle
    2. 6.2. A Controller Walk-Through
      1. 6.2.1. Setting Up Routes
        1. 6.2.1.1. Routing Basics
        2. 6.2.1.2. Named Routes
        3. 6.2.1.3. RESTful Routes and Resources
        4. 6.2.1.4. Configuring Routes for the Blog Application
      2. 6.2.2. Revisiting the Scaffold Generator
      3. 6.2.3. Rendering Responses
      4. 6.2.4. Redirecting
      5. 6.2.5. Understanding Templates
      6. 6.2.6. Working with Layouts
      7. 6.2.7. Looking at the Article Form
      8. 6.2.8. Using Form Helpers
      9. 6.2.9. Processing Request Parameters
      10. 6.2.10. Revisiting the Controller
      11. 6.2.11. Displaying Error Messages in Templates
      12. 6.2.12. Edit and Update actions
      13. 6.2.13. Revisiting the views
      14. 6.2.14. Staying DRY with Partials
        1. 6.2.14.1. Local Variable Assignment in Partials
        2. 6.2.14.2. Rendering an Object Partial
        3. 6.2.14.3. Rendering a Collection of Partials
    3. 6.3. Summary
  12. 7. Advanced Action Pack
    1. 7.1. Generating a Controller
    2. 7.2. Nested Resources
    3. 7.3. Sessions and the Login/Logout Logic
      1. 7.3.1. Lying in State
      2. 7.3.2. The Shared-Nothing Architecture
      3. 7.3.3. Storing Sessions in the Database
      4. 7.3.4. Using the Session
      5. 7.3.5. Session as a Resource
      6. 7.3.6. Logging In a User
      7. 7.3.7. Logging Out a User
    4. 7.4. Improving Controllers and Templates
      1. 7.4.1. Cleaning Up the Articles Index Page
      2. 7.4.2. Adding Categories to the Article Form
    5. 7.5. Using Controller Filters
      1. 7.5.1. Requiring Authentication with Filters
      2. 7.5.2. Applying Filters to Controllers
    6. 7.6. Adding Finishing Touches
      1. 7.6.1. Using Action View Helpers
      2. 7.6.2. Escaping HTML in Templates
      3. 7.6.3. Formatting the Body Field
      4. 7.6.4. Adding Edit Controls
      5. 7.6.5. Making Sure Articles Have Owners
      6. 7.6.6. Adding Custom Helpers
      7. 7.6.7. Giving It Some Style
        1. 7.6.7.1. Updating the Layout
        2. 7.6.7.2. Applying a Style Sheet
    7. 7.7. Summary
  13. 8. Improving Interaction with Ajax
    1. 8.1. Ajax and Rails
      1. 8.1.1. Prototype and jQuery
        1. 8.1.1.1. Installing jQuery
      2. 8.1.2. jQuery and DOM
    2. 8.2. Moving to Practice
      1. 8.2.1. Not All Users Comment
        1. 8.2.1.1. Loading a Template via Ajax
        2. 8.2.1.2. Responding to Requests with :format => :js
        3. 8.2.1.3. Making a Grand Entrance
      2. 8.2.2. Using Ajax for Forms
      3. 8.2.3. Deleting Records with Ajax
    3. 8.3. Summary
  14. 9. Sending and Receiving E-Mail
    1. 9.1. Setting Up Action Mailer
      1. 9.1.1. Configuring Mail Server Settings
      2. 9.1.2. Configuring Application Settings
    2. 9.2. Sending E-Mail
      1. 9.2.1. Handling Basic E-Mail
      2. 9.2.2. Sending HTML E-Mail
      3. 9.2.3. Adding Attachments
      4. 9.2.4. Letting Authors Know About Comments
    3. 9.3. Receiving E-Mail
      1. 9.3.1. Using a Rails Process
      2. 9.3.2. Reading E-Mail Using POP or IMAP
    4. 9.4. Summary
  15. 10. Testing Your Application
    1. 10.1. How Rails Handles Testing
    2. 10.2. Unit Testing Your Rails Application
      1. 10.2.1. Testing the Article Model
        1. 10.2.1.1. Creating Fixtures
        2. 10.2.1.2. Adding a Create Test
        3. 10.2.1.3. Testing with Assertions
        4. 10.2.1.4. Adding a Find Test
        5. 10.2.1.5. Adding an Update Test
        6. 10.2.1.6. Adding a Destroy Test
      2. 10.2.2. Testing Validations
    3. 10.3. Functional Testing Your Controllers
      1. 10.3.1. Testing the Articles Controller
      2. 10.3.2. Creating a Test Helper
        1. 10.3.2.1. Testing the Index Action
        2. 10.3.2.2. Testing the Show Action
        3. 10.3.2.3. Testing the New Action
        4. 10.3.2.4. Testing the Create Action
        5. 10.3.2.5. Testing the Destroy Action
        6. 10.3.2.6. One Missing Test
      3. 10.3.3. Running the Full Test Suite
    4. 10.4. Integration Testing
      1. 10.4.1. Integration-Testing the Blog Application
      2. 10.4.2. Story-Based Testing
    5. 10.5. Running the Full Test Suite
    6. 10.6. Summary
  16. 11. Internationalization
    1. 11.1.
      1. 11.1.1. Internationalization Logic in Rails
      2. 11.1.2. Setting Up i18n in the Blog Application
      3. 11.1.3. Localizing the Blog Application to Brazilian Portuguese
      4. 11.1.4. Bilingual Blog
    2. 11.2. Summary
  17. 12. Extending Rails with Plug-ins
    1. 12.1. Finding and Installing Plug-ins
      1. 12.1.1. Finding Plug-ins
      2. 12.1.2. Installing Plug-ins
    2. 12.2. Using a Plug-in in Your Application
      1. 12.2.1. Modifying the Database
      2. 12.2.2. Modifying the Application to Use the Plug-in
    3. 12.3. Creating Your Own Plug-in
      1. 12.3.1. Creating the Plug-in Module
      2. 12.3.2. Making the Plug-in Available to Applications
      3. 12.3.3. Using SimpleSearch
      4. 12.3.4. Testing the Plug-in
      5. 12.3.5. Updating the Controller and Views
    4. 12.4. Summary
  18. 13. Deploying Your Rails Applications
    1. 13.1. Deploying with Capistrano
      1. 13.1.1. Capistrano Installation
      2. 13.1.2. Capistrano Recipes
      3. 13.1.3. Capistrano on the Deployment Server
      4. 13.1.4. Custom Capistrano Tasks
    2. 13.2. Setting Up Your Server Architecture
      1. 13.2.1. Modular Architecture
        1. 13.2.1.1. Picking a Web Server
      2. 13.2.2. Becoming an Instant Deployment Expert
    3. 13.3. Summary
  19. A. Ruby, a Programmer's Best Friend
    1. A.1. Instant Interaction
    2. A.2. Ruby Data Types
      1. A.2.1. Strings
      2. A.2.2. Numbers
      3. A.2.3. Symbols
      4. A.2.4. Arrays and Hashes
    3. A.3. Language Basics
      1. A.3.1. Variables
      2. A.3.2. Operators
      3. A.3.3. Blocks and Iterators
      4. A.3.4. Control Structures
      5. A.3.5. Methods
    4. A.4. Classes and Objects
      1. A.4.1. Objects
      2. A.4.2. Classes
    5. A.5. Ruby Documentation
  20. B. Databases 101
    1. B.1. Examining a Database Table
    2. B.2. Working with Tables
      1. B.2.1. Selecting Data
      2. B.2.2. Inserting Data
      3. B.2.3. Updating Data
      4. B.2.4. Deleting Data
    3. B.3. Understanding Relationships
    4. B.4. SQL and Active Record
  21. C. The Rails Community
    1. C.1. Beginning Rails 3 Channels
    2. C.2. Rails Mailing Lists
    3. C.3. Rails IRC Channel
    4. C.4. Rails Blogs and Podcasts
    5. C.5. Rails Guides
    6. C.6. Rails Wiki
    7. C.7. Rails APIs
    8. C.8. Rails Source and Issue Tracking
    9. C.9. Working with Rails Directory
  22. D. Git
    1. D.1. What Is Source Control Management?
    2. D.2. How Does It Work?
    3. D.3. Git
      1. D.3.1. Installing Git
        1. D.3.1.1. Installing on Windows
        2. D.3.1.2. Installing on Mac OS X
        3. D.3.1.3. Installing on Linux
      2. D.3.2. Setting Global Parameters
      3. D.3.3. Initializing a Repository
      4. D.3.4. Ignoring Files
      5. D.3.5. Adding and Committing
      6. D.3.6. Branching and Merging
      7. D.3.7. Remote Repositories and Cloning
      8. D.3.8. Learning More
    4. D.4. Other SCM Systems
    5. D.5. Online Resources