O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Rails Cookbook

Book Description

Rails Cookbook is packed with the solutions you need to be aproficient developer with Rails, the leading framework for buildingthe new generation of Web 2.0 applications. Recipes range from thebasics, like installing Rails and setting up your developmentenvironment, to the latest techniques, such as developing RESTfulweb services.With applications that are code light, feature-full and built toscale quickly, Rails has revolutionized web development. TheRails Cookbook addresses scores of real-world challenges; eachone includes a tested solution, plus a discussion of how and why itworks, 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 andAjax

  • Ensuring that your application is security and performswell

  • 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'lldiscover ways to test, debug and secure your applications,incorporate Ajax, use caching to improve performance, and put yourapplication into production. Want to get ahead of the Web 2.0curve? This valuable cookbook will save you hundreds of hours whendeveloping applications with Rails.

    Table of Contents

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