You are previewing Ruby on Rails® Bible.
O'Reilly logo
Ruby on Rails® Bible

Book Description

Thanks to the explosive growth in popularity of the Rails framework, the equally popular Ruby programming language now has a great place to hang its hat. The powerful combination of the two provides the perfect toolset to create Web applications that feature concise code, clean syntax, and easy maintenance. This must-have book is your best guide on how to jump on the RoR bandwagon—from the basics of Ruby programming to advanced techniques for experienced Rails developers.

Table of Contents

  1. Copyright
  2. About the Author
  3. Credits
  4. Acknowledgments
  5. Introduction
  6. Quick Start: Ruby on Rails Quick Start
    1. Installing Instant Rails on Windows
    2. Installing Ruby and Rails on Mac OS X and Linux
      1. Installing on Linux
      2. Installing on Mac OS X
    3. Setting up a Development Environment
      1. Source code version control
      2. IDE or Editor?
        1. TextMate
        2. E
        3. IntelliJ IDEA
        4. NetBeans
        5. Eclipse
        6. Heroku
        7. Aptana Studio
    4. Summary
  7. I. First Steps with Rails
    1. 1. Learning Ruby
      1. 1.1. The Nature of Ruby
        1. 1.1.1. Dynamic or static typing
        2. 1.1.2. Duck typing
        3. 1.1.3. Compiled or scripting language
          1. 1.1.3.1. Compiled languages
          2. 1.1.3.2. Scripted languages
      2. 1.2. Object Oriented Programming
      3. 1.3. The Basics of Ruby
        1. 1.3.1. Ruby's interactive shell
        2. 1.3.2. Ruby syntax basics
          1. 1.3.2.1. Adding comments
          2. 1.3.2.2. Using parentheses
          3. 1.3.2.3. Using white space
          4. 1.3.2.4. Using semicolons
        3. 1.3.3. Running Ruby programs
      4. 1.4. Classes, Objects, and Variables
        1. 1.4.1. Using objects in Ruby
          1. 1.4.1.1. Defining objects
          2. 1.4.1.2. Writing methods
          3. 1.4.1.3. Methods with parameters
          4. 1.4.1.4. Creating instances of a class
          5. 1.4.1.5. Instance and class methods
          6. 1.4.1.6. Instance and class variables
          7. 1.4.1.7. Getters and setters in Ruby objects
        2. 1.4.2. Inheritance
      5. 1.5. Built-in Classes and Modules
        1. 1.5.1. Scalar objects
          1. 1.5.1.1. Strings
          2. 1.5.1.2. Numerics
          3. 1.5.1.3. Symbols
          4. 1.5.1.4. Times and dates
        2. 1.5.2. Collections
          1. 1.5.2.1. Arrays
          2. 1.5.2.2. Hashes
          3. 1.5.2.3. Ranges
      6. 1.6. Control Flow
        1. 1.6.1. Conditionals
          1. 1.6.1.1. The if statement
          2. 1.6.1.2. The unless statement
          3. 1.6.1.3. The case statement
        2. 1.6.2. Loops, blocks, and iterators
          1. 1.6.2.1. for loops
          2. 1.6.2.2. while and until loops
          3. 1.6.2.3. Blocks
          4. 1.6.2.4. Iterators
        3. 1.6.3. Exception handling
          1. 1.6.3.1. Exceptions in Ruby
          2. 1.6.3.2. Using begin, raise, and rescue
          3. 1.6.3.3. More exception handling using ensure, retry, and else
      7. 1.7. Organizing Code with Modules
      8. 1.8. Advanced Ruby Techniques
        1. 1.8.1. Variable length argument lists
        2. 1.8.2. Dynamic programming with method_missing
        3. 1.8.3. Reopening classes
      9. 1.9. Summary
    2. 2. Getting Started with Rails
      1. 2.1. What is Ruby on Rails?
        1. 2.1.1. DRY
        2. 2.1.2. Convention over configuration
        3. 2.1.3. Opinionated software
      2. 2.2. Rails Architecture
        1. 2.2.1. MVC
        2. 2.2.2. Rails and MVC
      3. 2.3. Rails Scripts
        1. 2.3.1. Rails Console
        2. 2.3.2. WEBrick
        3. 2.3.3. Generators
        4. 2.3.4. Migrations
      4. 2.4. Your First Rails Application
        1. 2.4.1. Create the project
        2. 2.4.2. Set up the database
        3. 2.4.3. Create the model
        4. 2.4.4. Create the controller and views
          1. 2.4.4.1. Implementing the index action
          2. 2.4.4.2. Implementing the new action
          3. 2.4.4.3. Implementing the Create action
          4. 2.4.4.4. Implementing the show action
          5. 2.4.4.5. Implementing the update action
          6. 2.4.4.6. What you have accomplished
        5. 2.4.5. Style the application
        6. 2.4.6. WebScaffolding
        7. 2.4.7. More to Get You Started
      5. 2.5. Summary
  8. II. Rails In Depth
    1. 3. Using Active Record
      1. 3.1. What is Active Record?
      2. 3.2. Active Record Basics
        1. 3.2.1. Active Record Classes and Objects
        2. 3.2.2. Active Record naming conventions
          1. 3.2.2.1. Class and table names
          2. 3.2.2.2. Table keys
      3. 3.3. Setting up a Model
        1. 3.3.1. Generate a Rails project and model
        2. 3.3.2. Configure Active Record
        3. 3.3.3. Rails Development Environments
      4. 3.4. Using Migrations
        1. 3.4.1. Schema versions
        2. 3.4.2. Migration methods
          1. 3.4.2.1. Tables
          2. 3.4.2.2. Columns
          3. 3.4.2.3. Indexes
        3. 3.4.3. Inserting data with migrations
      5. 3.5. Create, Read, Update, and Delete
        1. 3.5.1. Creating records
        2. 3.5.2. Reading data
          1. 3.5.2.1. Column metadata
          2. 3.5.2.2. Accessing attributes
          3. 3.5.2.3. Using the find method
          4. 3.5.2.4. Dynamic finders
          5. 3.5.2.5. Find using SQL
        3. 3.5.3. Creating and finding data with the Rails Console
        4. 3.5.4. Updating records
        5. 3.5.5. Deleting records
        6. 3.5.6. Using development log files
      6. 3.6. Defining Relationships
        1. 3.6.1. One-to-one relationships
          1. 3.6.1.1. Methods added by has_one
          2. 3.6.1.2. Methods added by belongs_to
        2. 3.6.2. Many-to-one relationships
          1. 3.6.2.1. Methods added by has_many
          2. 3.6.2.2. Methods added by belongs_to
        3. 3.6.3. Many-to-many relationships
          1. 3.6.3.1. Methods added by has_and_belongs_to_many
      7. 3.7. Implementing Validations
      8. 3.8. Custom Validations
      9. 3.9. Advanced Active Record
        1. 3.9.1. Single table inheritance
        2. 3.9.2. Composition
        3. 3.9.3. Transactions
      10. 3.10. Summary
    2. 4. Controller: In Depth
      1. 4.1. What is ActionController?
      2. 4.2. All About Routing
        1. 4.2.1. Defining custom routes
        2. 4.2.2. Defining a custom default route
        3. 4.2.3. Using named routes
        4. 4.2.4. Constructing URLs with url_for
      3. 4.3. Creating and Using Controllers
        1. 4.3.1. Generating controllers
        2. 4.3.2. Action methods
          1. 4.3.2.1. Using request parameters
          2. 4.3.2.2. Rendering templates
          3. 4.3.2.3. Redirects
          4. 4.3.2.4. Sending feedback with flash
          5. 4.3.2.5. Sending other types of data to the browser
      4. 4.4. Using Filters
        1. 4.4.1. Before filters
        2. 4.4.2. After filters
        3. 4.4.3. Around filters
        4. 4.4.4. Protecting filter methods
      5. 4.5. Working with Sessions
        1. 4.5.1. Using the ActiveRecord session storage
        2. 4.5.2. Using MemCached session storage
      6. 4.6. Caching
        1. 4.6.1. Page caching
        2. 4.6.2. Action caching
        3. 4.6.3. Fragment caching
      7. 4.7. Summary
    3. 5. View: In Depth
      1. 5.1. ActionView
        1. 5.1.1. Getting to the view
        2. 5.1.2. Rendering options
        3. 5.1.3. Responding to different formats
      2. 5.2. Embedded Ruby
        1. 5.2.1. Using the <%- and -%> delimiters
        2. 5.2.2. Commenting out embedded Ruby
      3. 5.3. Layouts
      4. 5.4. Partials
      5. 5.5. Helpers
        1. 5.5.1. Predefined Rails Helpers
          1. 5.5.1.1. HTML Creation Helpers
          2. 5.5.1.2. Form Creation Helpers
          3. 5.5.1.3. JavaScript Creation Helpers
          4. 5.5.1.4. Data Processing Helpers
          5. 5.5.1.5. Debugging Helpers
        2. 5.5.2. Creating Your Own Block Helpers
      6. 5.6. JavaScript, Ajax, and RJS
        1. 5.6.1. Prototype Helpers
        2. 5.6.2. RJS helpers
      7. 5.7. Summary
  9. III. Developing a Complete Rails Application
    1. 6. Developing Book Shelf: The Basic Framework
      1. 6.1. Application Overview
      2. 6.2. Creating a Skeleton for the Application
        1. 6.2.1. Begin the Book Shelf project
        2. 6.2.2. Setting up the databases
          1. 6.2.2.1. Create the databases
      3. 6.3. Create a Home Page
        1. 6.3.1. Create the Home controller
        2. 6.3.2. Create a layout and view
          1. 6.3.2.1. The HTML head
          2. 6.3.2.2. The body header section
          3. 6.3.2.3. The body sidebar section
          4. 6.3.2.4. The body content section
          5. 6.3.2.5. Creating the index view template
          6. 6.3.2.6. Testing the home page
          7. 6.3.2.7. Add some style
        3. 6.3.3. Set up a default route
      4. 6.4. Implementing Users
        1. 6.4.1. Create the user model
          1. 6.4.1.1. Securing user passwords
          2. 6.4.1.2. Generate the user model
          3. 6.4.1.3. Create the user migration
          4. 6.4.1.4. Add user model validations
        2. 6.4.2. Implement user registration
          1. 6.4.2.1. Create a user controller
          2. 6.4.2.2. Handle the user password
          3. 6.4.2.3. Create a registration view
          4. 6.4.2.4. Create user home view
        3. 6.4.3. Implement login and logout
          1. 6.4.3.1. Create login action method
          2. 6.4.3.2. Create the logout action method
          3. 6.4.3.3. Create an application login partial
          4. 6.4.3.4. Test the login and logout functionality
        4. 6.4.4. Using a before filter to protect pages
          1. 6.4.4.1. Modify the application controller
          2. 6.4.4.2. Add login form to the signup page
      5. 6.5. Summary
    2. 7. Developing Book Shelf: Adding the Core Functionality
      1. 7.1. Adding Support for Books
      2. 7.2. Refactor the sidebar code
      3. 7.3. Integrating with Amazon
        1. 7.3.1. Install Ruby/Amazon
        2. 7.3.2. Implement the Book Shelf-Amazon interface
          1. 7.3.2.1. The initialize method
          2. 7.3.2.2. The find_by_keyword method
      4. 7.4. Implementing a Search
        1. 7.4.1. Create the book search form
        2. 7.4.2. Generate the book controller and search action
        3. 7.4.3. Create the book model
          1. 7.4.3.1. Generate the book model class
          2. 7.4.3.2. Create the book migration
          3. 7.4.3.3. Run the migration
          4. 7.4.3.4. Associate the book model and the user model
        4. 7.4.4. Implement search logic in the book model
        5. 7.4.5. Create the search results page
        6. 7.4.6. Implement search results paging
      5. 7.5. Implementing the Addition and Deletion of Books
        1. 7.5.1. Adding a book
        2. 7.5.2. Deleting a book
          1. 7.5.2.1. Add the delete action
          2. 7.5.2.2. Update the page with RJS
      6. 7.6. Displaying a User's Books
      7. 7.7. Implementing the Book Detail Page
      8. 7.8. Summary
    3. 8. Developing Book Shelf: Social Support
      1. 8.1. Adding Social Support
      2. 8.2. Implementing Tagging
        1. 8.2.1. Installing a tagging plugin
          1. 8.2.1.1. Install the acts_as_taggable_redux plugin
        2. 8.2.2. Setting up the database for tagging support
        3. 8.2.3. Adding tagging support to the models
        4. 8.2.4. Adding tagging support to the controllers
        5. 8.2.5. Creating the view layer for tagging
          1. 8.2.5.1. Implementing the sidebar tag cloud view
          2. 8.2.5.2. Implement tag_cloud_revised helper method
          3. 8.2.5.3. Generate the tagging style sheet
          4. 8.2.5.4. Implement the static tag view
          5. 8.2.5.5. Make the tags editable
          6. 8.2.5.6. Implement the show_for_tag view
        6. 8.2.6. Using tags
      3. 8.3. Implementing Book Reviews
        1. 8.3.1. Implementing the review model
        2. 8.3.2. Adding associations to the book and user models
        3. 8.3.3. Implementing the review view
          1. 8.3.3.1. Displaying reviews for a book
          2. 8.3.3.2. Implementing the review entry form
        4. 8.3.4. Implementing the review controller
        5. 8.3.5. Adding some style
        6. 8.3.6. Adding a book review
      4. 8.4. Implementing Book Ratings
      5. 8.5. Extending the Application
        1. 8.5.1. Improving the user interface
        2. 8.5.2. Implementing an administrator interface
        3. 8.5.3. Adding RSS feeds to the application
        4. 8.5.4. Adding support for other book information sources
        5. 8.5.5. Implementing recommendations and suggestions
        6. 8.5.6. Expanding user profiles and adding avatars
      6. 8.6. Summary
    4. 9. Testing the Book Shelf Application
      1. 9.1. Why Test?
      2. 9.2. Using Test::Unit
        1. 9.2.1. Test assertions
        2. 9.2.2. Test fixtures
        3. 9.2.3. Test methods
        4. 9.2.4. Test runners
        5. 9.2.5. Test suites
      3. 9.3. Testing in Rails
        1. 9.3.1. Rails test directory
        2. 9.3.2. Rails test lifecycle
      4. 9.4. Setting Up a Test Database
      5. 9.5. Functional Tests
        1. 9.5.1. Running a test
        2. 9.5.2. Creating a test
          1. 9.5.2.1. Setting up fixtures
          2. 9.5.2.2. Writing a test method
          3. 9.5.2.3. More assertion methods
          4. 9.5.2.4. Verifying your test
        3. 9.5.3. Adding more tests
          1. 9.5.3.1. Add some reviews
          2. 9.5.3.2. Verify the tests again
      6. 9.6. Unit Tests
        1. 9.6.1. Setting up user fixtures
        2. 9.6.2. Test authentication
        3. 9.6.3. Test validations
          1. 9.6.3.1. Test valid password
          2. 9.6.3.2. Test valid login
          3. 9.6.3.3. Test valid e-mail
      7. 9.7. Integration Tests
      8. 9.8. Running All Tests
      9. 9.9. Test Coverage
      10. 9.10. Debugging Techniques
        1. 9.10.1. The Rails log files
        2. 9.10.2. Console-based
        3. 9.10.3. Using the debugger
      11. 9.11. Summary
  10. IV. Advanced Rails
    1. 10. Using Prototype and script.aculo.us
      1. 10.1. Prototype, script.aculo.us, and Rails
        1. 10.1.1. Using Prototype and script.aculo.us from Rails
        2. 10.1.2. Create a Rails project
          1. 10.1.2.1. Include the Prototype and script.aculo.us files
      2. 10.2. Prototype Overview
        1. 10.2.1. Ruby's influence on Prototype
        2. 10.2.2. What is Prototype?
      3. 10.3. Extensions to JavaScript
        1. 10.3.1. Simplifying JavaScript with the dollar sign
          1. 10.3.1.1. Selecting elements with $
          2. 10.3.1.2. Selecting elements with $$
          3. 10.3.1.3. Creating arrays with $A
          4. 10.3.1.4. Splitting strings with $w
          5. 10.3.1.5. Getting form field values with $F
          6. 10.3.1.6. Creating hashes with $H
          7. 10.3.1.7. Creating ranges with $R
        2. 10.3.2. More powerful arrays
          1. 10.3.2.1. Enumerating an array
        3. 10.3.3. JSON support
      4. 10.4. OOP with Prototype
        1. 10.4.1. Defining classes and inheritance
        2. 10.4.2. Implementing class inheritance with Prototype
      5. 10.5. Event Handling
      6. 10.6. Ajax
        1. 10.6.1. Ajax links
        2. 10.6.2. Ajax link options
      7. 10.7. script.aculo.us Overview
      8. 10.8. Visual Effects
        1. 10.8.1. script.aculo.us effects
        2. 10.8.2. Effect options
        3. 10.8.3. Using combination effects
      9. 10.9. Controls
        1. 10.9.1. Sliders
        2. 10.9.2. Auto-completion
        3. 10.9.3. In-place editing
          1. 10.9.3.1. Implementing a single value in-place editor
      10. 10.10. Drag and Drop
        1. 10.10.1. Creating draggable elements
          1. 10.10.1.1. Draggable options
        2. 10.10.2. Creating droppable elements
          1. 10.10.2.1. Droppable options
        3. 10.10.3. Sortable lists
      11. 10.11. JavaScript Testing
        1. 10.11.1. Creating JavaScript unit tests
        2. 10.11.2. Running JavaScript unit tests
      12. 10.12. Summary
    2. 11. Extending Rails
      1. 11.1. Beyond the Core
      2. 11.2. Generators
        1. 11.2.1. The generator directory structure
        2. 11.2.2. Writing generator code
        3. 11.2.3. Creating the templates
        4. 11.2.4. Running the new authentication generator
        5. 11.2.5. Extending Rails::Generator::NamedBase
      3. 11.3. Plugins
        1. 11.3.1. Using the Plugin script
          1. 11.3.1.1. List available plugins
          2. 11.3.1.2. List plugin sources
          3. 11.3.1.3. Adding and removing plugin sources
          4. 11.3.1.4. Discover new plugin sources
          5. 11.3.1.5. Installing, removing, and updating plugins
        2. 11.3.2. Writing a plugin
          1. 11.3.2.1. Write a new plugin
          2. 11.3.2.2. Try out the new plugin
        3. 11.3.3. Common techniques used to develop plugins
          1. 11.3.3.1. Extending classes with mixins
          2. 11.3.3.2. Opening a class
          3. 11.3.3.3. Dynamic extension with callbacks and hooks
          4. 11.3.3.4. Using code generation
        4. 11.3.4. Managing plugins with Piston
      4. 11.4. Summary of Useful Plugins
        1. 11.4.1. acts_as_rateable
        2. 11.4.2. Pagination
          1. 11.4.2.1. Installing will_paginate
          2. 11.4.2.2. Adding pagination to your application
        3. 11.4.3. acts_as_state_machine
          1. 11.4.3.1. Installing acts_as_state_machine
          2. 11.4.3.2. Using acts_as_state_machine
        4. 11.4.4. annotate_models
          1. 11.4.4.1. Installing annotate_models
          2. 11.4.4.2. Using annotate_models
        5. 11.4.5. exception_notifier
          1. 11.4.5.1. Installing exception_notifier
          2. 11.4.5.2. Using exception_notifier
        6. 11.4.6. resource_controller
          1. 11.4.6.1. Installing resource_controller
          2. 11.4.6.2. Using resource_controller
        7. 11.4.7. Adding user authentication
          1. 11.4.7.1. Installing restful_authentication
          2. 11.4.7.2. Using restful_authentication
        8. 11.4.8. Enhanced scaffolding
          1. 11.4.8.1. Streamlined
          2. 11.4.8.2. ActiveScaffold
        9. 11.4.9. Implementing content tagging
        10. 11.4.10. Handling file uploads
          1. 11.4.10.1. attachment_fu
      5. 11.5. Engines
        1. 11.5.1. Install the Engines plugin
        2. 11.5.2. Generate the engine skeleton
        3. 11.5.3. Move your application files into the engine
        4. 11.5.4. Modify your environment
        5. 11.5.5. Include your engine in your application
      6. 11.6. Summary
    3. 12. Advanced Topics
      1. 12.1. Beyond the Basics
      2. 12.2. RESTful Rails
        1. 12.2.1. Some advantages of RESTful architecture
        2. 12.2.2. REST as a Web service architecture
        3. 12.2.3. REST and representations
        4. 12.2.4. Writing a RESTful application with Rails
          1. 12.2.4.1. Rails routing and REST
          2. 12.2.4.2. PUT and DELETE full disclosure
          3. 12.2.4.3. Generate a RESTful resource
      3. 12.3. Working with Legacy Databases
        1. 12.3.1. Override database table and field names
        2. 12.3.2. Side by side with the legacy database
      4. 12.4. Using ActionMailer
        1. 12.4.1. Configuring a Rails application for e-mail support
        2. 12.4.2. Generating a mailer model
        3. 12.4.3. Writing code to send e-mail
        4. 12.4.4. Writing code to receive e-mail
      5. 12.5. ActiveResource and XML
      6. 12.6. Deploying with Capistrano
        1. 12.6.1. Installing and setting up Capistrano
        2. 12.6.2. Running basic Capistrano tasks
        3. 12.6.3. Customizing Capistrano
      7. 12.7. Summary
  11. V. Appendixes
    1. A. Ruby Quick Reference
      1. A.1. Basic Ruby Syntax
        1. A.1.1. Literal expressions
          1. A.1.1.1. Arrays
          2. A.1.1.2. Boolean literals
          3. A.1.1.3. Hashes
          4. A.1.1.4. Numbers
          5. A.1.1.5. Ranges
          6. A.1.1.6. Regular expressions
          7. A.1.1.7. Strings
          8. A.1.1.8. Symbols
        2. A.1.2. Variable and method names
        3. A.1.3. Operators
        4. A.1.4. Method calls
        5. A.1.5. Special keyword expressions
          1. A.1.5.1. The if expression
          2. A.1.5.2. The unless expression
          3. A.1.5.3. The case expression
          4. A.1.5.4. The for expression
          5. A.1.5.5. The while expression
          6. A.1.5.6. The until expression
          7. A.1.5.7. Loop control keywords
        6. A.1.6. Assignment
        7. A.1.7. File input and output
        8. A.1.8. Exceptions
      2. A.2. Objects and Classes
        1. A.2.1. Defining methods
        2. A.2.2. Blocks
        3. A.2.3. Defining classes and modules
          1. A.2.3.1. Defining modules
          2. A.2.3.2. Defining classes
          3. A.2.3.3. Superclasses and self
          4. A.2.3.4. Including and extending with modules
          5. A.2.3.5. Attributes
          6. A.2.3.6. Access control
        4. A.2.4. Method lookup
    2. B. Ruby on Rails Guide
      1. B.1. Getting Started
        1. B.1.1. Standard Rails application
        2. B.1.2. Generators
      2. B.2. Controllers and Helpers
        1. B.2.1. Traditional routing
        2. B.2.2. RESTful routing
        3. B.2.3. Controller variables
        4. B.2.4. Filters
        5. B.2.5. Rendering and redirecting
        6. B.2.6. Respond to
        7. B.2.7. Helpers
      3. B.3. Views
        1. B.3.1. ERb
        2. B.3.2. RJS
      4. B.4. Models
        1. B.4.1. Creating
        2. B.4.2. Reading
        3. B.4.3. Updating
        4. B.4.4. Deleting
        5. B.4.5. Relationships
          1. B.4.5.1. belongs_to
          2. B.4.5.2. has_one
          3. B.4.5.3. has_many
          4. B.4.5.4. has_and_belongs_to_many
      5. B.5. Database Migrations
      6. B.6. Plugins