Build Your Own Ruby on Rails Web Applications

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Want to learn all about Ruby on Rails, the web application framework that is inspiring developers around the world? This practical hands-on guide for first-time Ruby on Rails programmers will walk you through installing the required software on a Windows, Mac or Linux computer. And before you get coding, an entire chapter is devoted to object oriented programming in Ruby, so you

Author(s): Patrick Lenz
Edition: 1
Publisher: SitePoint
Year: 2007

Language: English
Pages: 447

Build Your Own Ruby On Rails Web Applications......Page 1
Table of Contents......Page 9
Who Should Read this Book?......Page 17
What’s in this Book?......Page 18
Updates and Errata......Page 20
Acknowledgements......Page 21
Introducing Ruby on Rails......Page 23
History......Page 25
Convention Over Configuration......Page 27
Agile Development......Page 29
What is digg?......Page 31
Features of the Example Application......Page 33
Summary......Page 35
Getting Started......Page 37
What Does All this Cost?......Page 38
Installing on Windows......Page 39
Installing on Mac OS X......Page 41
Installing Xcode......Page 42
Introducing the Command Line......Page 43
Setting the Path......Page 45
Installing Ruby on a Mac......Page 46
Installing RubyGems on a Mac......Page 48
Installing Rails on a Mac......Page 49
Installing MySQL on a Mac......Page 51
Using a Package Manager......Page 53
Prerequisites......Page 54
Installing Ruby on Linux......Page 55
Installing RubyGems on Linux......Page 56
Installing Rails on Linux......Page 57
Installing MySQL on Linux......Page 58
One Directory Structure to Rule them All......Page 62
Creating the Standard Directory Structure......Page 63
Starting our Application......Page 65
Which Text Editor?......Page 67
ConTEXT......Page 68
TextMate......Page 69
TextWrangler......Page 70
Summary......Page 71
Ruby is a Scripting Language......Page 73
The Great Performance Debate......Page 74
Ruby is an Object Oriented Language......Page 75
The Interactive Ruby Shell (irb)......Page 79
Literal Objects......Page 80
Variables and Constants......Page 81
Dot Notation......Page 82
Use of Parentheses......Page 83
Method Notation......Page 84
Classes and Objects......Page 85
Instance Variables......Page 86
Instance Methods......Page 87
Accessor Methods......Page 88
Class-level Functionality......Page 89
Inheritance......Page 91
Return Values......Page 92
Standard Output......Page 93
Arrays......Page 94
Hashes......Page 96
Strings......Page 97
Numerics......Page 99
nil......Page 100
Control Structures......Page 101
Conditionals......Page 102
The if Construct......Page 103
The unless Construct......Page 104
while and until Loops......Page 105
for Loops......Page 106
Blocks......Page 107
Summary......Page 109
Three Environments......Page 111
The Database Configuration File......Page 113
Creating the Databases......Page 115
The Model-view-controller Architecture......Page 116
MVC in Theory......Page 117
MVC the Rails Way......Page 118
ActiveRecord (the Model)......Page 119
Database Tables......Page 120
Saving an Object......Page 122
Defining Relationships Between Objects......Page 124
ActionController (the Controller)......Page 125
ActionView (the View)......Page 127
Code Generation......Page 129
Automated Testing......Page 131
Debugging......Page 132
A GUI Tool for MySQL......Page 133
Summary......Page 136
The Model Generator......Page 139
Running the generate Script......Page 140
Understanding the Output......Page 141
What’s YAML?......Page 143
Creating a Skeleton Migration File......Page 144
Filling in the Gaps......Page 146
Using rake to Migrate our Data......Page 147
Managing Data Using the Rails Console......Page 149
Creating Records......Page 150
Retrieving Records......Page 151
Updating Records......Page 153
Where’s the SQL?......Page 154
Running the generate Script......Page 156
Understanding the Output......Page 158
Starting our Application … Again......Page 159
Generating Views with Scaffolding......Page 161
Creating Dynamic Pages......Page 164
Passing Data Back and Forth......Page 165
Pulling in a Model......Page 166
Summary......Page 168
Calling Upon our Trusty Helpers......Page 171
Story Submission......Page 172
Introducing the form_for Helper......Page 173
Creating the Template......Page 174
Modifying the Controller......Page 176
Analyzing the HTML......Page 177
Saving Data to the Database......Page 178
Establishing Structure......Page 180
Adding some Style......Page 182
Adding to the Flash......Page 183
Retrieving Data from the Flash......Page 184
Tweaking the Redirection Logic......Page 186
Improving the User Experience......Page 187
Testing the Form......Page 189
Analyzing the Skeleton File......Page 190
Writing a Unit Test......Page 191
Running a Unit Test......Page 193
Testing the Controller......Page 195
Writing a Functional Test......Page 196
Running a Functional Test......Page 198
More Functional Tests......Page 199
Running the Complete Test Suite......Page 202
Revisiting the Logs......Page 203
Summary......Page 205
Generating a Vote Model......Page 207
Creating the Migration......Page 208
Applying the Migration......Page 209
Introducing the has_many Clause......Page 210
Introducing the belongs_to Clause......Page 213
How’s our Schema Looking?......Page 214
Implementing Clean URLs......Page 215
Adding Permalinks to Stories......Page 218
Showing off our Stories......Page 220
Ajax and Rails......Page 223
What is Ajax?......Page 224
Visual Effects with script.aculo.us......Page 225
Making Stories Shove-able......Page 227
Including the JavaScript Libraries......Page 228
Giving Stories a Shove......Page 229
Styling the Scoreboard......Page 230
Introducing RJS Templates......Page 232
Ensuring Graceful Degradation......Page 235
Introducing Partials......Page 237
Adding Voting History......Page 238
Styling the Voting History......Page 239
Tweaking the Voting History......Page 242
Preparing the Fixtures......Page 244
Testing a Story’s Relationship to a Vote......Page 245
Testing a Vote’s Relationship to a Story......Page 246
Testing the Voting History Order......Page 247
Testing the Controller......Page 248
Testing Page Rendering......Page 249
Testing Ajax Voting......Page 251
Testing Regular HTTP Voting......Page 252
Running the Full Test Suite......Page 253
Summary......Page 254
Sessions and Cookies......Page 255
What’s a Cookie?......Page 256
What’s a Session?......Page 258
Sessions in Rails......Page 259
Generating a User Model......Page 260
Adding Relationships for the User Class......Page 262
Creating a User......Page 263
Creating the Controller......Page 264
Creating the View......Page 265
Adding Functionality to the Controller......Page 267
Before Filters......Page 269
A Word on Filter Methods......Page 270
Retrieving the Current User......Page 272
Displaying the Name of the Current User......Page 274
Allowing Users to Log Out......Page 276
Adding a Navigation Menu......Page 277
Abstracting Code Using helper_method......Page 279
Requiring Users to Log In......Page 280
Introducing Filter Conditions......Page 281
Redirecting the User......Page 282
Storing the Submitter......Page 283
Displaying the Submitter......Page 284
Testing the Model......Page 285
Preparing the Fixtures......Page 286
Testing a User’s Relationship to a Story......Page 287
Testing a User’s Relationship to a Vote......Page 288
Testing a Vote’s Relationship to a User......Page 289
Testing the Display of the Login Form......Page 290
Testing a Successful Login......Page 291
Testing a Failed Login......Page 292
Testing a Logout......Page 293
Testing the Display of the Story Submitter......Page 294
Testing Display of the Username......Page 295
Testing Story Submission......Page 297
Testing Storage of the Submitter......Page 298
Running the Full Test Suite......Page 299
Summary......Page 301
Promoting Popular Stories......Page 303
Introducing the Counter Cache......Page 304
Making Room for the Cache......Page 305
Implementing the Front Page......Page 307
Modifying the View......Page 308
Modifying the Partial......Page 309
Setting the Default Page......Page 310
Implementing the Voting Bin......Page 311
Introducing ActionView Helpers......Page 314
Writing an ActionView Helper......Page 315
Expanding the Navigation Menu......Page 316
Requiring that Users Log In to Vote......Page 318
Adding Story Attributes......Page 320
Introducing Model Callbacks......Page 322
Adding a Callback......Page 324
Expanding the Submission Form......Page 325
Adding User Pages......Page 327
Introducing the has_many :through Association......Page 328
Modifying the Controller......Page 329
Creating the View......Page 330
Testing the Model......Page 332
Testing Additions to the Counter Cache......Page 333
Testing Deletions from the Counter Cache......Page 334
Testing the Generation of Permalinks......Page 335
Testing the Join Model Relationship......Page 336
Testing the Story Index Pages......Page 337
Testing the Routing Configuration......Page 338
Testing the Story Submission Form......Page 339
Testing User Voting History......Page 340
Testing the Story Submitter Link Text......Page 341
Testing the AccountController......Page 342
Summary......Page 343
What is a Plugin?......Page 345
Adding Tagging to Shovell......Page 347
Installing the acts_as_taggable Plugin......Page 348
Creating a Migration for the Plugin......Page 349
Understanding Polymorphic Associations......Page 352
Making a Model Taggable......Page 353
Enabling Tag Submission......Page 355
Modifying the Controller......Page 356
Modifying the View......Page 357
Updating the story Partial......Page 358
Assigning our First Tags......Page 359
Displaying Tagged Stories......Page 361
Testing the Tagging Functionality......Page 362
Testing the Finding of a Story by Tag......Page 363
Testing the Submission of a New Story with Tags......Page 364
Testing the tag Action for Listing Tagged Stories......Page 365
Testing the Display of the Story Submission Form......Page 366
Running the Test Suite ... Again!......Page 367
Summary......Page 368
Debugging your Application......Page 369
Debugging within Templates......Page 370
Debugging Using Breakpoints......Page 377
Using the Rails Logging Tool......Page 382
When to Use an Integration Test......Page 385
Creating our First Integration Test......Page 386
Using Breakpoints in a Test......Page 388
Revisiting the Rails Console......Page 391
Benchmarking your Application......Page 393
Taking Benchmarks from Log Files......Page 394
Manual Benchmarking......Page 397
Summary......Page 398
The Implications of “Production”......Page 399
Choosing a Production Environment......Page 401
Web Server Options......Page 402
lightTPD......Page 403
The mod_ruby Extension......Page 404
Mongrel......Page 405
Step 1: Installing Mongrel......Page 406
Step 2: Moving to the Production System......Page 409
Step 3: Setting Up Apache......Page 415
Alternatives for Session Storage......Page 417
The ActiveRecord Store Session Container......Page 418
The MemCached Store Session Container......Page 419
Caching......Page 420
Capistrano......Page 421
Summary......Page 422
Index......Page 425