Cover image for Learning Rails

Book description

While most books written about Rails cater to programmers looking for information on data structures, Learning Rails targets web developers whose programming experience is tied directly to the Web. Rather than begin with the inner layers of a Rails web application--the models and controllers--this unique book approaches Rails development from the outer layer: the application interface. You'll learn how to create something visible with Rails before reaching the more difficult database models and controller code. With Learning Rails, you can start from the foundations of web design you already know, and then move more deeply into Ruby, objects, and database structures. This book will help you:

  • Present web content by building an application with a basic view and a simple controller, while learning Ruby along the way

  • Build forms and process their results, progressing from the simple to the more complex

  • Connect forms to models by setting up a database, and use Rails' ActiveRecord to create code that maps to database structures

  • Use Rails scaffolding to build applications from a view-centric perspective

  • Add common web application elements such as sessions, cookies, and authentication

  • Build applications that combine data from multiple tables

  • Create simple but dynamic interfaces with Rails and Ajax

Once you complete Learning Rails, you'll be comfortable working with the Rails web framework, and you'll be well on your way to becoming a Rails guru.

Table of Contents

  1. Learning Rails
    1. A Note Regarding Supplemental Files
    2. Preface
      1. Who This Book Is For
      2. Who This Book Is Not For
      3. What You’ll Learn
      4. Ruby and Rails Style
      5. Other Options
      6. Rails Versions
      7. If You Have Problems Making Examples Work
      8. If You Like (or Don’t Like) This Book
      9. Conventions Used in This Book
      10. Using Code Examples
      11. How to Contact Us
      12. Acknowledgments
    3. 1. Starting Up Ruby on Rails
      1. Getting Started in the Online Cloud: Heroku
      2. Getting Started with Instant Rails
      3. Getting Started at the Command Line
        1. Starting Up Rails
        2. Dodging Database Issues
      4. What Server Is That?
      5. Test Your Knowledge
        1. Quiz
        2. Answers
    4. 2. Rails on the Web
      1. Creating Your Own View
      2. What Are All Those Folders?
      3. Adding Some Data
      4. How Hello World Works
      5. Protecting Your View from the Controller
      6. Parentheses Are (Usually) Optional
      7. Adding Logic to the View
      8. Test Your Knowledge
        1. Quiz
        2. Answers
    5. 3. Adding Web Style
      1. I Want My CSS!
      2. Layouts
        1. Splitting View from Layout
        2. Creating a Default Layout
        3. Choosing a Layout from a Controller
        4. Sharing Template Data with the Layout
      3. Setting a Default Page
      4. Test Your Knowledge
        1. Quiz
        2. Answers
    6. 4. Controlling Data Flow: Controllers and Models
      1. Getting Started, Greeting Guests
      2. Application Flow
      3. Keeping Track: A Simple Guestbook
        1. Connecting to a Database Through a Model
        2. Connecting the Controller to the Model
          1. Storing data using the model
          2. Retrieving data from the model and showing it
      4. Finding Data with ActiveRecord
      5. Test Your Knowledge
        1. Quiz
        2. Answers
    7. 5. Accelerating Development with Scaffolding and REST
      1. A First Look at Scaffolding
      2. REST and Controller Best Practices
        1. Websites and Web Applications
        2. Toward a Cleaner Approach
      3. Examining a RESTful Controller
        1. Index: An Overview of Data
        2. Show: Just One Row of Data
        3. New: A Blank Set of Data Fields
        4. Edit: Hand Me That Data, Please
        5. Create: Save Something New
        6. Put This Updated Record In
        7. Destroy It
      4. Escaping the REST Prison
      5. Test Your Knowledge
        1. Quiz
        2. Answers
    8. 6. Presenting Models with Forms
      1. More Than a Name on a Form
      2. Generating HTML Forms with Scaffolding
      3. Form As a Wrapper
      4. Creating Text Fields and Text Areas
      5. Creating Checkboxes
      6. Creating Radio Buttons
      7. Creating Selection Lists
      8. Dates and Times
      9. Labels
      10. Creating Helper Methods
        1. Letting Helper Methods Make Choices
        2. A More Elegant Helper Method
      11. Putting the Form Body in a Partial
      12. Test Your Knowledge
        1. Quiz
        2. Answers
    9. 7. Strengthening Models with Validation
      1. Without Validation
      2. The Original Model
      3. The Power of Declarative Validation
      4. Managing Secrets
        1. Customizing the Message
        2. Limiting Choices
        3. Testing Format with Regular Expressions
        4. Seen It All Before
        5. Numbers Only
      5. A Place on the Calendar
        1. Testing for Presence
      6. Beyond Simple Declarations
        1. Test It Only If
        2. Do It Yourself
      7. Test Your Knowledge
        1. Quiz
        2. Answers
    10. 8. Improving Forms
      1. Adding a Picture by Uploading a File
        1. File Upload Forms
        2. Model and Migration Changes
          1. A migration for an extension
          2. Extending a model beyond the database
          3. Showing it off
        3. Results
      2. Standardizing Your Look with Form Builders
        1. Supporting Your Own Field Types
        2. Adding Automation
        3. Integrating Form Builders and Styles
      3. Test Your Knowledge
        1. Quiz
        2. Answers
    11. 9. Developing Model Relationships
      1. Connecting Awards to Students
        1. Establishing the Relationship
        2. Supporting the Relationship
        3. Guaranteeing a Relationship
      2. Connecting Students to Awards
        1. Removing Awards When Students Disappear
        2. Counting Awards for Students
      3. Nesting Awards in Students
        1. Changing the Routing
        2. Changing the Controller
        3. Changing the Award Views
        4. Connecting the Student Views
        5. Is Nesting Worth It?
      4. Many-to-Many: Connecting Students to Courses
        1. Creating Tables
        2. Connecting the Models
        3. Adding to the Controllers
        4. Adding Routing
        5. Supporting the Relationship Through Views
          1. Establishing navigation
          2. Showing counts
          3. Enrolling students in courses
      5. What’s Missing?
      6. Test Your Knowledge
        1. Quiz
        2. Answers
    12. 10. Managing Databases with Migrations
      1. What Migrations Offer You
      2. Migration Basics
        1. Migration Files
        2. Running Migrations Forward and Backward
      3. Inside Migrations
        1. Working with Tables
        2. Data Types
        3. Working with Columns
        4. Indexes
        5. Other Opportunities
      4. Test Your Knowledge
        1. Quiz
        2. Answers
    13. 11. Debugging
      1. Creating Your Own Debugging Messages
      2. Logging
      3. Working with Rails from the Console
      4. The Ruby Debugger
      5. Test Your Knowledge
        1. Quiz
        2. Answers
    14. 12. Testing
      1. Test Mode
      2. Setting Up a Test Database with Fixtures
      3. Unit Testing
      4. Functional Testing
        1. Calling Controllers
        2. Testing Responses
        3. Dealing with Nested Resources
      5. Integration Testing
      6. Beyond the Basics
      7. Test Your Knowledge
        1. Quiz
        2. Answers
    15. 13. Sessions and Cookies
      1. Getting Into and Out of Cookies
      2. Storing Data Between Sessions
      3. Test Your Knowledge
        1. Quiz
        2. Answers
    16. 14. Users and Authentication
      1. Installation
      2. Storing User Data
      3. Controlling Sessions
      4. Classifying Users
      5. More Options
      6. Test Your Knowledge
        1. Quiz
        2. Answers
    17. 15. Routing
      1. Creating Routes to Interpret URIs
        1. Specifying Routes with map.connect
        2. A Domain Default with map.root
        3. Route Order and Priority
        4. Named Routes
        5. Globbing
        6. Regular Expressions and Routing
        7. Mapping Resources
        8. Nesting Resources
        9. Checking the Map
      2. Generating URIs from Views and Controllers
        1. Pointing url_for in the Right Direction
        2. Adding Options
      3. Infinite Possibilities
      4. Test Your Knowledge
        1. Quiz
        2. Answers
    18. 16. Creating Dynamic Interfaces with Rails and Ajax
      1. Ajax Basics
      2. Supporting Ajax with Rails
        1. Rails as a Server API
        2. Rails and the Client
      3. Managing Enrollment through Ajax
        1. Making the Form More Scriptable
        2. Changing Courses without Changing Pages
        3. Rethinking Logic
      4. Moving Further into Ajax
      5. Test Your Knowledge
        1. Quiz
        2. Answers
    19. 17. Mail in Rails
      1. Sending Text Mail
        1. Setup
        2. Adjusting Routing for an email Method
        3. Sending Email
      2. Sending HTML Mail
      3. Sending Complex HTML Email
      4. Receiving Mail
        1. Setup
        2. Processing Messages
      5. Test Your Knowledge
        1. Quiz
        2. Answers
    20. 18. Securing, Managing, and Deploying Your Rails Projects
      1. Securing Your Application
        1. SQL Injection
        2. Cross-Site Scripting
        3. Cross-Site Request Forgery (CSRF)
        4. URL Hacking
        5. Other Security Issues
      2. Deploying Rails Applications
        1. Changing to Production Mode
        2. Database, Web, and App Servers
          1. Database choices
          2. Web server choices
          3. App server choices
        3. Walking Through a Passenger-Based Deployment
          1. Creating the MySQL database
          2. Configuring Rails to use the MySQL database
          3. A quick test
          4. Installing Phusion Passenger
          5. Results
        4. Deployment Directions
      3. Test Your Knowledge
        1. Quiz
        2. Answers
    21. 19. Making the Most of Rails—And Beyond
      1. Keep Up with Rails
      2. Plug-ins
      3. Ruby
      4. Web Services
      5. Explore Other Ruby Frameworks
      6. Migrating Legacy Applications to Rails
      7. Keep Exploring
    22. A. An Incredibly Brief Introduction to Ruby
      1. How Ruby Works
      2. How Rails Works
      3. Getting Started with Classes and Objects
      4. Comments
      5. Variables, Methods, and Attributes
        1. Variables
          1. Arrays and hashes
          2. Symbols
        2. Methods
          1. Privacy, please
          2. super
          3. Calling methods: advanced options
        3. Attributes
      6. Logic and Conditionals
        1. Operators
        2. if, else, unless, and elsif
        3. ?:
        4. case and when
        5. Loops
          1. while and until
          2. Just Counting
          3. for
        6. Many More Possibilities
    23. B. An Incredibly Brief Introduction to Relational Databases
      1. Tables of Data
        1. Connecting Tables
        2. Using Tables to Connect Tables
        3. Granularity
      2. Databases, Tables, and Rails
    24. C. An Incredibly Brief Guide to Regular Expressions
      1. What Regular Expressions Do
      2. Starting Small
      3. The Simplest Expressions: Literal Strings
      4. Character Classes
      5. Escaping
      6. Modifiers
      7. Anchors
      8. Sequences, Repetition, Groups, and Choices
      9. Greed
      10. More
    25. D. A Catalog of Helper Methods
      1. Calling Helper Methods
      2. ActiveRecordHelper
      3. AssetTagHelper
      4. AtomFeedHelper and AtomFeedHelper::AtomFeedBuilder
      5. BenchmarkHelper, CacheHelper, and CaptureHelper
      6. DateHelper
      7. DebugHelper
      8. FormHelper, FormTagHelper, and FormOptionsHelper
      9. JavaScriptHelper
      10. NumberHelper
      11. PrototypeHelper
      12. RecordIdentificationHelper
      13. SanitizeHelper
      14. ScriptaculousHelper
      15. TagHelper
      16. TextHelper
      17. UrlHelper
    26. E. Glossary
      1. Speaking in Rails
    27. Index
    28. About the Authors
    29. Colophon