beginning rails 3

403 1.2K 0
beginning rails 3

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

this print for content only—size & color not accurate 7.5 x 9.25 spine = 0.75" 400 page count Carneiro Al Barazi Beginning Rails 3 THE EXPERT’S VOICE ® IN WEB DEVELOPMENT Beginning Rails 3 CYAN MAGENTA YELLOW BLACK PANTONE 123 C Cloves Carneiro Jr., and Rida Al Barazi Companion eBook Available A beginner’s guide to building web applications with Ruby on Rails BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Cloves Carneiro Jr., Author of Beginning Rails US $39.99 Shelve in: Rails/Web Development User level: Beginner www.apress.com www.github. com/ccjr/blog/ downloads www.github. com/ccjr/pizzaria SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-2433-4 9 781430 224334 5 39 9 9 Rida Al Barazi Beginning Rails 3 Dear Reader, This book is for anyone who wants to learn how to develop Web applications using the Ruby on Rails Web framework. Starting with a thorough introduction to the Rails 3 landscape, we'll walk you through creating a couple of working applications, from installation to deployment, and everything in between. Rails 3 is modular for a reason: software is easier to write and maintain when it's divided according to areas of concern. Each chapter in this book is designed around a specific component of the framework. From modeling database rela- tionships with Active Record, to sending mail using Action Mailer, we show you how each of the components work together and where the lines of responsibil- ity are drawn. The result is that your knowledge will be focused, and you'll know precisely how the pieces fit together. Rails 3 is optimized for what most people need to do, most of the time, and this book is no different. We've focused on the techniques that we, as Rails developers, use on a daily basis. With the help of online tools, such as Github, it’s easy to follow along and use the code in the book. After building a blog application throughout the book, we've created an e-commerce Web site in the last chapter, using a faster pace, and demonstrating the thought process that we use when building applications for real clients. In this book you'll find advice, and practical solutions to common problems. We'll show you what we think are the best tools and techniques to use when working with Rails, and what we've found to be best practices. We hope you'll find it useful, informative, and even entertaining. Cloves Carneiro Jr. and Rida Al Barazi THE APRESS ROADMAP Beginning Ruby on Rails E-Commerce Practical Rails Plugins Practical Rails Projects Beginning Ruby: From Novice to Professional Beginning Rails 3 www.it-ebooks.info www.it-ebooks.info Beginning Rails 3                  Cloves Carneiro Jr. Rida Al Barazi www.it-ebooks.info Beginning Rails 3 Copyright © 2010 by Cloves Carneiro Jr. and Rida Al Barazi All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2433-4 ISBN-13 (electronic): 978-1-4302-2434-1 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Ben Renow-Clarke, Jim Markham Technical Reviewer: Eldon Almeda Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Jim Markham Copy Editor: Tiffany Taylor Compositor: Bytheway Publishing Services Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com , or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. www.it-ebooks.info For my family, Jane and Noah. —Cloves For my parents, Mezyan Al Barazi and Lina Jano. I wouldn’t be here without your support. —Rida www.it-ebooks.info  CONTENTS iv Contents at a Glance  About the Authors xv  About the Technical Reviewer xvi  Acknowledgments xvii  Introduction xviii  Chapter 1: Introducing the Rails Framework 1  Chapter 2: Getting Started 13  Chapter 3: Getting Something Running 31  Chapter 4: Working with a Database: Active Record 51  Chapter 5: Advanced Active Record: Enhancing Your Models 73  Chapter 6: Action Pack: Working with the View and the Controller 121  Chapter 7: Advanced Action Pack 157  Chapter 8: Improving Interaction with Ajax 201  Chapter 9: Sending and Receiving E-Mail 215  Chapter 10: Testing Your Application 233  Chapter 11: Internationalization 269  Chapter 12: Extending Rails with Plug-ins 285  Chapter 13: Deploying Your Rails Applications 307  Appendix A: Ruby, a Programmer’s Best Friend 317  Appendix B: Databases 101 333  Appendix C: The Rails Community 341  Appendix D: Git 345  Index 361 www.it-ebooks.info  CONTENTS v Contents  About the Authors xv  About the Technical Reviewer xvi  Acknowledgments xvii  Introduction xviii  Chapter 1: Introducing the Rails Framework 1 The Rise and Rise of the Web Application 1 The Web Isn’t Perfect 2 The Good Web Framework 2 Enter Rails 3 Rails Is Ruby 4 Rails Encourages Agility 5 Rails Is Opinionated Software 7 Rails Is Open Source 7 The MVC Pattern 8 The MVC Cycle 8 The Layers of MVC 9 The Libraries That Make Up Rails 11 Rails Is Modular 12 Rails Is No Silver Bullet 12 Summary 12 www.it-ebooks.info  CONTENTS vi  Chapter 2: Getting Started 13 An Overview of Rails Installation 13 Installing on Mac OS X 10.6 Snow Leopard 14 Installing the Apple Developer Tools (Xcode) 14 Updating RubyGems and Installing Rails 15 Installing on Windows 16 Installing Ruby 16 Installing Rails 17 Installing SQLite 18 Installing on Linux 19 Installing Ruby 19 Updating RubyGems 20 Installing Rails 20 Installing SQLite 21 Creating Your First Rails Application 21 Starting the Built-In Web Server 22 Generating a Controller 25 Creating an Action 26 Creating a Template 26 Summary 29  Chapter 3: Getting Something Running 31 An Overview of the Project 31 Creating the Blog Application 31 Creating the Project Databases 34 Creating the Article Model 36 Creating a Database Table 38 Generating a Controller 41 Up and Running with Scaffolding 42 www.it-ebooks.info  CONTENTS vii Adding More Fields 44 Adding Validations 46 Generated Files 48 Summary 50  Chapter 4: Working with a Database: Active Record 51 Introducing Active Record: Object-Relational Mapping on Rails 52 What About SQL? 53 Active Record Conventions 54 Introducing the Console 54 Active Record Basics: CRUD 57 Creating New Records 58 Reading (Finding) Records 61 Updating Records 66 Deleting Records 67 When Good Models Go Bad 69 Summary 71  Chapter 5: Advanced Active Record: Enhancing Your Models 73 Adding Methods 73 Using Associations 76 Declaring Associations 77 Creating One-to-One Associations 78 Creating One-to-Many Associations 83 Applying Association Options 89 Creating Many-to-Many Associations 90 Creating Rich Many-to-Many Associations 94 Advanced Finding 97 Using the where Method 97 Using a SQL Fragment 97 www.it-ebooks.info  CONTENTS viii Using an Array Condition Syntax 98 Using Association Proxies 100 Other Finder Methods 100 Default Scope 102 Named Scope 103 Applying Validations 105 Using Built-in Validations 105 Building Custom Validation Methods 109 Making Callbacks 111 Observers 113 Updating the User Model 115 Reviewing the Updated Models 119 Summary 120  Chapter 6: Action Pack: Working with the View and the Controller 121 Action Pack Components 121 Action Controller 122 Action View 124 Embedded Ruby 125 Helpers 126 Routing 126 RESTful Resources 127 The Action Pack Request Cycle 128 A Controller Walk-Through 129 Setting Up Routes 129 Revisiting the Scaffold Generator 132 Rendering Responses 137 Redirecting 137 Understanding Templates 138 www.it-ebooks.info [...]... 33 7 Deleting Data 33 7 Understanding Relationships .33 8 SQL and Active Record 34 0 Appendix C: The Rails Community 34 1 Beginning Rails 3 Channels 34 1 Rails Mailing Lists 34 1 Rails IRC Channel 34 2 Rails Blogs and Podcasts .34 2 Rails Guides 34 3 Rails Wiki .34 3 Rails APIs... 32 3 Control Structures 32 5 Methods 32 6 Classes and Objects .32 7 Objects 32 7 Classes 32 8 Ruby Documentation .33 0 Appendix B: Databases 101 33 3 Examining a Database Table 33 3 Working with Tables 33 4 Selecting Data 33 5 Inserting Data 33 6 Updating... 30 0 Summary .30 6 Chapter 13: Deploying Your Rails Applications 30 7 Deploying with Capistrano 30 7 Capistrano Installation 30 8 Capistrano Recipes 31 0 Capistrano on the Deployment Server 31 2 Custom Capistrano Tasks 31 3 Setting Up Your Server Architecture 31 3 Modular Architecture 31 3 Becoming... .34 3 Rails Source and Issue Tracking 34 3 Working with Rails Directory 34 4 xiii www.it-ebooks.info CONTENTS Appendix D: Git 34 5 What Is Source Control Management? 34 5 How Does It Work? 34 5 Git 34 6 Installing Git 34 6 Setting Global Parameters 34 7 Initializing a Repository 34 8 Ignoring... 31 4 Summary .31 5 Appendix A: Ruby, a Programmer’s Best Friend .31 7 Instant Interaction 31 7 Ruby Data Types 31 8 Strings 31 8 Numbers 31 9 Symbols 32 0 Arrays and Hashes 32 0 Language Basics 32 1 Variables 32 2 Operators 32 3 xii www.it-ebooks.info... 229 Receiving E-Mail 230 Using a Rails Process 231 x www.it-ebooks.info CONTENTS Reading E-Mail Using POP or IMAP 231 Summary . 232 Chapter 10: Testing Your Application 233 How Rails Handles Testing 233 Unit Testing Your Rails Application 235 Testing the Article Model 236 Testing Validations ... Initializing a Repository 34 8 Ignoring Files 35 0 Adding and Committing 35 0 Branching and Merging 35 2 Remote Repositories and Cloning 35 7 Learning More 35 8 Other SCM Systems .35 9 Online Resources 36 0 Index 36 1 xiv www.it-ebooks.info About the Authors Cloves Carneiro Jr is a software... conceptually easy to grasp—and therein lies its strength With this book, you’ll learn everything you need to know to get started building web applications with Rails 3 The Beginning Rails Website Be sure to check out this book’s website at http://beginningrails.com In addition to the most up-todate version of the source code used in the book, you’ll find errata, notes, tips, and other important updates You... rather, it means that in order to truly appreciate Rails, you may need a history lesson in the technologies from whose ashes Rails has risen Sometimes, until you’ve experienced the hurt, you can’t appreciate the cure Rails Is Open Source The Rails culture is steeped in open source tradition The Rails source code is, of course, open And it’s significant that Rails is licensed under the MIT license, arguably... familiar with Rails inner workings Rails Is No Silver Bullet There is no question that Rails offers web developers a lot of benefits After using Rails, it’s hard to imagine going back to web development without it Fortunately, it looks like Rails will be around for a long time, so there’s no need to worry But it brings us to an important point As much as we’ve touted the benefits of Rails, it’s important . Beginning Rails 3 Channels 34 1 Rails Mailing Lists 34 1 Rails IRC Channel 34 2 Rails Blogs and Podcasts 34 2 Rails Guides 34 3 Rails Wiki 34 3 Rails APIs 34 3 Rails Source and Issue Tracking 34 3 Working. Tables 33 4 Selecting Data 33 5 Inserting Data 33 6 Updating Data 33 7 Deleting Data 33 7 Understanding Relationships 33 8 SQL and Active Record 34 0  Appendix C: The Rails Community 34 1 Beginning. Iterators 32 3 Control Structures 32 5 Methods 32 6 Classes and Objects 32 7 Objects 32 7 Classes 32 8 Ruby Documentation 33 0  Appendix B: Databases 101 33 3 Examining a Database Table 33 3 Working

Ngày đăng: 24/04/2014, 14:42

Từ khóa liên quan

Mục lục

  • Prelim

  • Contents at a Glance

  • Contents

  • About the Authors

  • About the Technical Reviewer

  • Acknowledgments

  • Introduction

    • The Beginning Rails Website

    • Introducing the Rails Framework

      • The Rise and Rise of the Web Application

      • The Web Isn’t Perfect

      • The Good Web Framework

      • Enter Rails

        • Rails Is Ruby

        • Rails Encourages Agility

        • Less Software

        • Convention Over Configuration

        • Don’t Repeat Yourself

        • Rails Is Opinionated Software

        • Rails Is Open Source

        • The MVC Pattern

          • The MVC Cycle

          • The Layers of MVC

          • Models

Tài liệu cùng người dùng

Tài liệu liên quan