Cover image for Rails Cookbook

Book description

Rails Cookbook is packed with the solutions you need to be a proficient developer with Rails, the leading framework for building the new generation of Web 2.0 applications. Recipes range from the basics, like installing Rails and setting up your development environment, to the latest techniques, such as developing RESTful web services. With applications that are code light, feature-full and built to scale quickly, Rails has revolutionized web development. The Rails Cookbook addresses scores of real-world challenges; each one includes a tested solution, plus a discussion of how and why it works, so that you can adapt the techniques to similar situations. Topics include:

  • Modeling data with the ActiveRecord library

  • Setting up views with ActionView and RHTML templates

  • Building your application's logic into ActionController

  • Testing and debugging your Rails application

  • Building responsive web applications using JavaScript and Ajax

  • Ensuring that your application is security and performs well

  • Deploying your application with Mongrel and Apache

  • Using Capistrano to automate deployment

  • Using the many Rails plugins

  • Working with graphics

Whether you're new to Rails or an experienced developer, you'll discover ways to test, debug and secure your applications, incorporate Ajax, use caching to improve performance, and put your application into production. Want to get ahead of the Web 2.0 curve? This valuable cookbook will save you hundreds of hours when developing applications with Rails.

Table of Contents

  1. Rails Cookbook
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Foreword
    4. Preface
      1. Who This Book Is For
      2. Other Resources
        1. Web Sites
        2. Books
      3. Conventions Used in This Book
        1. Font Conventions
      4. Using Code Examples
      5. Safari® Enabled
      6. Comments and Questions
      7. Acknowledgments
    5. 1. Getting Started
      1. Introduction
      2. Joining the Rails Community
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Finding Documentation
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Installing MySQL
        1. Problem
        2. Solution
          1. Windows
          2. Linux
          3. Mac OS X
        3. Discussion
        4. See Also
      5. Installing PostgreSQL
        1. Problem
        2. Solution
          1. Windows
          2. Linux
          3. Mac OS X
        3. Discussion
        4. See Also
      6. Installing Rails
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Fixing Ruby and Installing Rails on OS X 10.4 Tiger
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Running Rails in OS X with Locomotive
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Running Rails in Windows with Instant Rails
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Updating Rails with RubyGems
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Getting Your Rails Project into Subversion
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    6. 2. Rails Development
      1. Introduction
      2. Creating a Rails Project
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Jump-Starting Development with Scaffolding
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Speeding Up Rails Development with Mongrel
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Enhancing Windows Development with Cygwin
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Understanding Pluralization Patterns in Rails
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Developing Rails in OS X with TextMate
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Cross-Platform Developing with RadRails
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Installing and Running Edge Rails
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Setting Up Passwordless Authentication with SSH
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Generating RDoc for Your Rails Application
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Creating Full-Featured CRUD Applications with Streamlined
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    7. 3. Active Record
      1. Introduction
      2. Setting Up a Relational Database to Use with Rails
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Programmatically Defining Database Schema
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Developing Your Database with Migrations
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Modeling a Database with Active Record
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Inspecting Model Relationships from the Rails Console
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Accessing Your Data via Active Record
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Retrieving Records with find
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Iterating Over an Active Record Result Set
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Retrieving Data Efficiently with Eager Loading
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Updating an Active Record Object
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Enforcing Data Integrity with Active Record Validations
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Executing Custom Queries with find_by_sql
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Protecting Against Race Conditions with Transactions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Adding Sort Capabilities to a Model with acts_as_list
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Performing a Task Whenever a Model Object Is Created
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Modeling a Threaded Forum with acts_as_nested_set
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Creating a Directory of Nested Topics with acts_as_tree
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Avoiding Race Conditions with Optimistic Locking
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Handling Tables with Legacy Naming Conventions
        1. Problem
        2. Solution
        3. Discussion
      21. Automating Record Timestamping
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Factoring Out Common Relationships with Polymorphic Associations
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. Mixing Join Models and Polymorphism for Flexible Data Modeling
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    8. 4. Action Controller
      1. Introduction
      2. Accessing Form Data from a Controller
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Changing an Application’s Default Page
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Clarifying Your Code with Named Routes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Configuring Customized Routing Behavior
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Displaying Alert Messages with Flash
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Extending the Life of a Flash Message
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Following Actions with Redirects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Generating URLs Dynamically
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Inspecting Requests with Filters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Logging with Filters
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Rendering Actions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Restricting Access to Controller Methods
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Sending Files or Data Streams to the Browser
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Storing Session Information in a Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Tracking Information with Sessions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Using Filters for Authentication
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    9. 5. Action View
      1. Introduction
      2. Simplifying Templates with View Helpers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Displaying Large Datasets with Pagination
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Creating a Sticky Select List
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Editing Many-to-Many Relationships with Multiselect Lists
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Factoring Out Common Display Code with Layouts
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Defining a Default Application Layout
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Generating XML with Builder Templates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Generating RSS Feeds from Active Record Data
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Reusing Page Elements with Partials
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Processing Dynamically Created Input Fields
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Customizing the Behavior of Standard Helpers
        1. Problem
        2. Solution
        3. Discussion
      13. Creating a Web Form with Form Helpers
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Formatting Dates, Times, and Currencies
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Personalizing User Profiles with Gravatars
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Avoiding Harmful Code in Views with Liquid Templates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Globalizing Your Rails Application
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    10. 6. RESTful Development
      1. Introduction
        1. REST Is a Conversation
        2. REST Is Design
        3. Rails and REST
      2. Creating Nested Resources
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Supporting Alternative Data Formats by MIME Type
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Modeling Relationships RESTfully with Join Models
        1. Problem
        2. Solution
        3. Discussion
      5. Moving Beyond Simple CRUD with RESTful Resources
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Consuming Complex Nested REST Resources
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Developing Your Rails Applications RESTfully
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    11. 7. Rails Application Testing
      1. Introduction
      2. Centralizing the Creation of Objects Common to Test Cases
        1. Problem
        2. Solution
        3. Discussion
      3. Creating Fixtures for Many-to-Many Associations
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Importing Test Data with CSV Fixtures
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Including Dynamic Data in Fixtures with ERb
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Initializing a Test Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Interactively Testing Controllers from the Rails Console
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Interpreting the Output of Test::Unit
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Loading Test Data with YAML Fixtures
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Monitoring Test Coverage with rake stats
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Running Tests with Rake
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Speeding Up Tests with Transactional Fixtures
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Testing Across Controllers with Integration Tests
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Testing Controllers with Functional Tests
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Examining the Contents of Cookie
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      16. Testing Custom and Named Routes
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      17. Testing HTTP Requests with Response-Related Assertions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      18. Testing a Model with Unit Tests
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      19. Unit Testing Model Validations
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      20. Verifying DOM Structure with Tag-Related Assertions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      21. Writing Custom Assertions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      22. Testing File Upload
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      23. Modifying the Default Behavior of a Class for Testing by Using Mocks
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      24. Improving Feedback by Running Tests Continuously
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      25. Analyzing Code Coverage with Rcov
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    12. 8. JavaScript and Ajax
      1. Introduction
      2. Adding DOM Elements to a Page
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Creating a Custom Report with Drag and Drop
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Dynamically Adding Items to a Select List
        1. Problem
        2. Solution
        3. Discussion
      5. Monitoring the Content Length of a Textarea
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Updating Page Elements with RJS Templates
        1. Problem
        2. Solution
        3. Discussion
      7. Inserting JavaScript into Templates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Letting a User Reorder a List
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Autocompleting a Text Field
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Searching for and Highlighting Text Dynamically
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Enhancing the User Interface with Visual Effects
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Implementing a Live Search
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Editing Fields in Place
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Creating an Ajax Progress Indicator
        1. Problem
        2. Solution
        3. Discussion
    13. 9. Action Mailer
      1. Introduction
      2. Configuring Rails to Send Email
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Creating a Custom Mailer Class with the Mailer Generator
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Formatting Email Messages Using Templates
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Attaching Files to Email Messages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Sending Email from a Rails Application
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Receiving Email with Action Mailer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    14. 10. Debugging Rails Applications
      1. Introduction
      2. Exploring Rails from the Console
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Fixing Bugs at the Source with Ruby -cw
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Debugging Your Application in Real Time with the breakpointer
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Logging with the Built-in Rails Logger Class
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Writing Debugging Information to a File
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Emailing Application Exceptions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Outputting Environment Information in Views
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Displaying Object Contents with Exceptions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Filtering Development Logs in Real Time
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Debugging HTTP Communication with Firefox Extensions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Debugging Your JavaScript in Real Time with the JavaScript Shell
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Debugging Your Code Interactively with ruby-debug
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    15. 11. Security
      1. Introduction
      2. Hardening Your Systems with Strong Passwords
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Protecting Queries from SQL Injection
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Guarding Against Cross-Site Scripting Attacks
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Restricting Access to Public Methods or Actions
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Securing Your Server by Closing Unnecessary Ports
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    16. 12. Performance
      1. Introduction
      2. Measuring Web Server Performance with Httperf
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Benchmarking Portions of Your Application Code
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Improving Performance by Caching Static Pages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Expiring Cached Pages
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Mixing Static and Dynamic Content with Fragment Caching
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Filtering Cached Pages with Action Caching
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Speeding Up Data Access Times with memcached
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Increasing Performance by Caching Post-Processed Content
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    17. 13. Hosting and Deployment
      1. Introduction
      2. Hosting Rails Using Apache 1.3 and mod_fastcgi
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Managing Multiple Mongrel Processes with mongrel_cluster
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Hosting Rails with Apache 2.2, mod_proxy_balancer, and Mongrel
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Deploying Rails with Pound in Front of Mongrel, Lighttpd, and Apache
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Customizing Pound’s Logging with cronolog
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Configuring Pound with SSL Support
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Simple Load Balancing with Pen
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Deploying Your Rails Project with Capistrano
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Deploying Your Application to Multiple Environments with Capistrano
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Deploying with Capistrano When You Can’t Access Subversion
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Deploying with Capistrano and mongrel_cluster
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      13. Disabling Your Web Site During Maintenance
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      14. Writing Custom Capistrano Tasks
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      15. Cleaning Up Residual Session Records
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    18. 14. Extending Rails with Plug-ins
      1. Introduction
      2. Finding Third-Party Plug-ins
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      3. Installing Plug-ins
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Manipulating Record Versions with acts_as_versioned
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Building Authentication with acts_as_authenticated
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Simplifying Folksonomy with the acts_as_taggable
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Extending Active Record with acts_as
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Adding View Helpers to Rails as Plug-ins
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      9. Uploading Files with file_column
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      10. Uploading Files with acts_as_attachment
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      11. Disabling Records Instead of Deleting Them with acts_as_paranoid
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      12. Adding More Elaborate Authentication Using the Login Engine
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    19. 15. Graphics
      1. Introduction
      2. Installing RMagick for Image Processing
        1. Problem
        2. Solution
          1. Windows
          2. Linux
          3. Mac OS X
        3. Discussion
        4. See Also
      3. Uploading Images to a Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      4. Serving Images Directly from a Database
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      5. Creating Resized Thumbnails with RMagick
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      6. Generating PDF Documents
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      7. Visually Displaying Data with Gruff
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
      8. Creating Small, Informative Graphs with Sparklines
        1. Problem
        2. Solution
        3. Discussion
        4. See Also
    20. A. Migrating to Rails 1.2
      1. Action Controller
      2. Active Record
      3. Action View
    21. Index
    22. About the Author
    23. Colophon
    24. SPECIAL OFFER: Upgrade this ebook with O’Reilly