9780596521424
index.html

Rails 3 in a Nutshell

Cody Fauser

James MacAulay

Edward Ocampo-Gooding

John Guenin

This work has been released under the Creative Commons Attribution-Noncommercial license.

Abstract

Note

You are reading the text of an O'Reilly book that's under development. The authors are publishing the book to this site as it's being written, and we're putting it here to get feedback from you. This book uses the Open Feedback Publishing System (OFPS), an O'Reilly experiment that tries to bridge the gap between private manuscripts and public blogs.

Next to every paragraph, there is a link you can use to comment on what you're reading. We are grateful for any feedback you have: questions, comments, suggestions, and corrections are all welcome and appreciated.

Rails in a Nutshell is a concise introduction to Rails, an overview of commands and configurations, and a guide to the parts of Rails you’ll be using every day.

Full of examples and explanations, this book kicks your skills into high-gear by showing you how to take advantage of the Model-View-Controller concept with tiny but expressive bits of Ruby that power some of the world’s biggest and fastest web services.

Fast to launch and a pleasure to get there are hallmarks of working with Rails. Rails in a Nutshell gets the right stuff in your hands quickly and without fuss, so you can experience it yourself.

Follow the progress of the book on Twitter twitter.com/railsnutshell, at railsinanutshell.com, and at the book's catalog page.


1. Preface
Intentions of the authors
2. Rails in a Nutshell
Architecture
Model
View
Controller
Mailer
Web Services
Plugins
Getting Started
Generate a Rails application
Start up your app
Generate a scaffolded resource
Running database migrations
Check out your working application
Setting up a default route
XSS protection
View helpers
Model validations
RSS feeds & request formats
View layouts
Routes
Model associations
Nested resources
Partials
AJAX
The Console
Summary
3. Active Model
Attributes
Tracking Changes
Validations
Built-in macros
4. Active Record
Your interfaces to Active Record
The database and its schema
Model definitions
Relations
Record instances
Connecting to a Database
A Model’s Names
Developing your Database Schema with Migrations
Type Mapping
Tables
Columns
Associations
belongs_to
has_one
Options
Better Names for your Associations
Polymorphic associations
Attributes
Tracking Changes
Validations
Built-in validations
Common options
Custom validations
When Validations Don’t Happen
Callbacks
Single Table Inheritance
Aggregation
Creating
Finding the records you want
Constructing Relations
where
select
group & having
order
limit & offset
includes
joins
lock
readonly
from
only & except
Calculations
Updating
Deleting
Transactions
Observers
5. Action Controller
Getting Started
Record Identification
Responding to Requests
The HEAD Response
Rendering a Response
Redirecting
Response Headers
Cookies
Working with Cookies
Permanent Cookies
Signed Cookies
Sessions
Using the session
Session Stores
Configuration
Flash
Storing Objects in the Flash
Accessing the Flash from View Templates
Limiting an Object to the Current Request
Keeping the Flash for an Extra Request
Discarding the Flash at the End of the Current Request
Controller Callbacks
Filters
After and Around Filters
Defining Filters
Limiting Filters to Particular Actions
Sending Files
6. Routing
Defining Routes
Getting a Feel for the Routing DSL
Resources
Routing Any Path You Like
Specifying Parameter Defaults
Simple Constraints
Advanced Constraints
Named Routes
Specifying a root Route
Scopes
Namespaces
Redirects
Generating URIs From Routes
7. The Asset Pipeline
Exploring the Pipeline
What You Get With a New Project
Controller Scripts and Stylesheets
The Asset Server in the Development Environment
Mapping Asset Paths
Compiling From Different Languages
Rails helpers
Precompilation
Serving Assets from a Dedicated Host
Configuring the Pipeline
Stages of the Pipeline
preprocess
compile
concatenate (directives)
compress
digest
where to put assets
linking to assets
debugging
digests
directives
precompilation
8. Testing
The Basics
Unit Tests
Assertions
Test Helper Methods
Fixtures
Functional Tests
View Tests
View Helper Tests
Routing Tests
Integration Tests
Creating an integration test
Integration testing commands
Testing the article workflow
Testing multiple sessions
Extending integration tests with helpers
Stubbing & Mocking
Stubbing a method
Stub & Mock Objects
Running tests
Using rake
Test environment
Debugging within a test
9. Caching
Caching Strategies
Under the Hood
Understanding Fragment Caching
Moving on to Action Caching
Sweepers
Page Caching
HTTP Caching
Using Caching Strategies
Our Blog
Step 1: Fragment Caching the View
Step 2: HTTP Caching in the Controller
Step 3: Caching Views Generated from Arrays
Step 5: Comments Touch Posts
Handling Code Changes
Static Assets
Static Asset Caching Strategies
Handling Static Assets in Production
Static Assets on Heroku (or any direct ruby process)
Static Assets with a Nginx/Apache
Moving Away from the HTTP Request
Setting the Stage
Tag Based Caching
Enter Cashier
Fast JSON APIs
Structuring a JSON API
Classes
Background Cache Warming
Tips and Tricks
CSRF and form_authenticty_token
Bringing Caching into the Model Layer
Dealing with Relative Dates (or other content)
Conclusion: Cashing Out
HTTP Caching
Page Caching
Action Caching
Fragment Caching
General Points
10. Rack
Getting Started
Environment
Middleware
Rackup
Using Rack in your Rails Application
The Rails Middleware Stack
Creating Custom Middleware
Installing Middleware
Routing to a Rack Application
11. Action Mailer
Sending Email
Mailers
Rendering the Email Body
Creating and Sending Mail
Generating URIs
HTML Email
Adding Attachments
Configuration
SMTP Delivery Options
Sendmail Delivery Options
File Delivery Options
Test Delivery Settings
Receiving Email
12. Active Resource
Background
Client and Server
Web Service Client
Web Service Server
Finding Resources
Finding all resources
Finding a single resource by ID
Finding resources from custom paths
Creating Resources
Creating a resource
Validation and Errors
Updating Resources
Deleting Resource
Making Custom Requests
HTTP Authentication
13. Active Support
Object
Class
Module
NilClass
Integer
Float
Numeric
Hash
Array
Range
Enumerable
Time, Date, and DateTime
Describing moments in time
Direct time duration manipulation
Is it in the future? Is it today? Is it in the past?
Time Arithmetic
Time Zone Conversions
Formatting Time-like objects
Days in a month
MessageVerifier
MessageEncryptor
SecureRandom
Inflections
StringInquirer
Multibyte
Base64
Gzip compression & decompression
Callbacks
Cache
Commonly Used Methods
Storage Implementations
Memoizable
JSON serialization
XML serialization
14. Internationalization
Locale Detection
Locale Persistence
Translation
Regional Locales and Fallbacks
Formatting
Pluralization
Localizing Views
Locale Organization
Backends
What else is out there?
15. Plugins
Installing Plugins
Writing a Plugin
Hooking into Active Record
16. Appendix
Configuration
Configuration
Environments
Action Mailer Configuration
Active Support Cache Store
Rails Initializers
Logging
Configuration
HTTP Status Codes
HTTP Status Code Mapping
Memcached
Installing Memcached
Site last updated on: December 11, 2012 at 10:21:28 PM PST
Cover for Rails 3 in a Nutshell

View 7 comments

  1. freethinker – Posted Oct. 5, 2010

    are the books docbook sources available?

  2. edward – Posted Oct. 16, 2010

    Hi Pratik,

    The docbook sources are heavily modified by some code-generation tools and other stuff that pieces it all together, so I imagine they wouldn't be that useful.

    Once the first edition is out, ping us again and we'll see if we can figure something out.

  3. mill mrmill – Posted March 7, 2012

    Your blog is great. Your thoughts are also very good and i am very inspired from your post. That is why I visit this blog again and again and will come back in future too.

    Edited on March 7, 2012, 6:17 p.m. PST

  4. petesurfer – Posted April 23, 2013

    A nice book and a very interesting project. I hope you will continue in this way. Also I have bookmarked this page. Much greetings Die Kanaren , Madeira Insel , Mexiko Land

  5. auei – Posted April 26, 2013

    I've never considered that surfing can be so much helpful and entertaining. I'm truly happy and grateful for sharing me with this kind of terrific points. Really an excellent page... you are only the best. videos gratis - sex shop - webcam porno - maduras - jovencitas - xxx - videos x - redtube - travestis - porno

  6. betto – Posted May 6, 2013

    There is some method to buy with paypal?

    Edited on May 6, 2013, 5:12 p.m. PDT

  7. auei – Posted May 12, 2013

    Hi betto,

    Send me an email to javimuro1@gmail.com I sell link on this web.

    I can also sell on other websites with high pagerank.

Add a comment

View 13 comments

  1. MikeSummers – Posted Oct. 21, 2009

    Will there be an Action View Chapter? A placeholder would be good.

  2. codyfauser – Posted Oct. 21, 2009

    Mike,

    There will be. The incomplete sections are currently not shown, but you're right, it would be good to have the table of contents complete with the entire structure. However, that might also make it frustrating if most parts are empty.

  3. blaix – Posted Oct. 27, 2009

    I think a complete table of contents would help so we're not worried something important might not be covered.

  4. alexbaum – Posted Oct. 28, 2009

    Hi. We would like to translate this book to russian lang, have we rights to do this? We start at http://railstranslate.novarchiv.org, but Wiki is not usable to do this :). Can you recommend any service for open-translate?

    PS: Thanks for this book :)

  5. alexbaum – Posted Oct. 28, 2009

    We are http://translated.by/you/rails-in-a-nutshell/ :)

  6. faisal – Posted Nov. 23, 2009

    Can we get a one-page version of the book (including comments) so we can load it into our Kindles or what have you?

  7. zawaideh – Posted Feb. 6, 2010

    I am unable to post comments next to the paragraphs.. is this the only place to do so?

  8. edward – Posted Oct. 16, 2010

    Hi folks,

    It looks like we missed responding to the questions in this thread. Sorry about that.

    Faisal: Thanks for the tip. The intro’s been updated, but needs another go-over now that Rails 3 is out. (I’ve been writing a few of the other chapters since.)

    Zaid: you should be able to add it to any paragraph. If you can’t, then that’s our fault. Any place in particular where you can’t?

    Constantine: it looks like there

  9. jeromewilson – Posted Nov. 7, 2010

    Attempting to access chapter 1 throws a server error :(

  10. guanxsi – Posted Nov. 7, 2010

    server error in chapter1 for 3 days already

  11. edwardog – Posted Nov. 8, 2010

    Sorry about that – we don’t get exception logs with this setup. It’s fixed now.

    Thanks for taking a look!

  12. Caro Hankmann – Posted Oct. 31, 2011

    Its nice

    Edited on October 31, 2011, 10:28 a.m. PDT

  13. mimim – Posted March 9, 2012

    This chapter seems to be good to know. I already learned some chapters from this.

Add a comment