Introduction - The Rails 4 Way (2014)

The Rails 4 Way (2014)

Introduction

It’s an exciting time for the Rails community. We have matured tremendously and our mainstream adoption continues to pick up steam. Nearly 10 years after DHH first started playing with Ruby, it’s safe to say that Rails remains a relevant and vital tool in the greater web technology ecosystem.

Rails 4 represents a big step forward for the community. We shed a variety of vestigial features that had been deprecated in Rails 3. Security was beefed up and raw performance improved. Most everything in the framework feels, well, tighter than before. Rails 4 is leaner and meaner than its previous incarnations, and so is this edition of The Rails Way.

In addition to normal revisions to bring the text up to date with the evolution of Rails’ numerous APIs, this edition adds a significant amount of new and updated material about Security, Performance and Caching, Haml, RSpec, Ajax and the new Asset Pipeline.

About This Book

As with previous editions, this book is not a tutorial or basic introduction to Ruby or Rails. It is meant as a day-to-day reference for the full-time Rails developer. The more confident reader might be able to get started in Rails using just this book, extensive online resources, and his wits, but there are other publications that are more introductory in nature and might be a wee bit more appropriate for beginners.

Every contributor to this book works with Rails on a full time basis. We do not spend our days writing books or training other people, although that is certainly something that we enjoy doing on the side.

This book was originally conceived for myself, because I hate having to use online documentation, especially API docs, which need to be consulted over and over again. Since the API documentation is liberally licensed (just like the rest of Rails), there are a few sections of the book that reproduce parts of the API documentation. In practically all cases, the API documentation has been expanded and/or corrected, supplemented with additional examples and commentary drawn from practical experience.

Hopefully you are like me—I really like books that I can keep next to my keyboard, scribble notes in, and fill with bookmarks and dog-ears. When I’m coding, I want to be able to quickly refer to both API documentation, in-depth explanations, and relevant examples.

Book Structure

I attempted to give the material a natural structure while meeting the goal of being the best-possible Rails reference book. To that end, careful attention has been given to presenting holistic explanations of each subsystem of Rails, including detailed API information where appropriate. Every chapter is slightly different in scope, and I suspect that Rails is now too big a topic to cover the whole thing in depth in just one book.

Believe me, it has not been easy coming up with a structure that makes perfect sense for everyone. Particularly, I have noted surprise in some readers when they notice that Active Record is not covered first. Rails is foremost a web framework and at least to me, the controller and routing implementation is the most unique, powerful, and effective feature, with Active Record following a close second.

Sample Code and Listings

The domains chosen for the code samples should be familiar to almost all professional developers. They include time and expense tracking, auctions, regional data management, and blogging applications. I don’t spend pages explaining the subtler nuances of the business logic for the samples or justify design decisions that don’t have a direct relationship to the topic at hand. Following in the footsteps of my series colleague Hal Fulton and The Ruby Way, most of the snippets are not full code listings—only the relevant code is shown. Ellipses (…) often denote parts of the code that have been eliminated for clarity.

Whenever a code listing is large and significant, and I suspect that you might want to use parts of it verbatim in your own code, I supply a listing heading. There are not too many of those. The whole set of code listings will not add up to a complete working system, nor are there 30 pages of sample application code in an appendix. The code listings should serve as inspiration for your production-ready work, but keep in mind that it often lacks touches necessary in real-world work. For example, examples of controller code are often missing pagination and access control logic, because it would detract from the point being expressed.

Some of the source code for my examples can be found at https://github.com/obie/tr3w_time_and_expenses. Note that it is not a working nor complete application. It just made sense at times to keep the code in the context of an application and hopefully you might draw some inspiration from browsing it.

Concerning 3rd-Party RubyGems and Plugins

Whenever you find yourself writing code that feels like plumbing, by which I mean completely unrelated to the business domain of your application, you’re probably doing too much work. I hope that you have this book at your side when you encounter that feeling. There is almost always some new part of the Rails API or a third-party RubyGem for doing exactly what you are trying to do.

As a matter of fact, part of what sets this book apart is that I never hesitate in calling out the availability of third-party code, and I even document the RubyGems and plugins that I feel are most crucial for effective Rails work. In cases where 3rd-party code is better than the built-in Rails functionality, we don’t cover the built-in Rails functionality (pagination is a good example).

An average developer might see his productivity double with Rails, but I’ve seen serious Rails developers achieve gains that are much, much higher. That’s because we follow the Don’t Repeat Yourself (DRY) principle religiously, of which Don’t Reinvent The Wheel (DRTW) is a close corollary. Reimplementing something when an existing implementation is good enough is an unnecessary waste of time that nevertheless can be very tempting, since it’s such a joy to program in Ruby.

Ruby on Rails is actually a vast ecosystem of core code, official plugins, and third-party plugins. That ecosystem has been exploding rapidly and provides all the raw technology you need to build even the most complicated enterprise-class web applications. My goal is to equip you with enough knowledge that you’ll be able to avoid continuously reinventing the wheel.

Recommended Reading and Resources

Readers may find it useful to read this book while referring to some of the excellent reference titles listed in this section.

Most Ruby programmers always have their copy of the “Pickaxe” book nearby, Programming Ruby (ISBN: 0-9745140-5-5), because it is a good language reference. Readers interested in really understanding all of the nuances of Ruby programming should acquire The Ruby Way, Second Edition (ISBN: 0-6723288-4-4).

I highly recommend Peepcode Screencasts, in-depth video presentations on a variety of Rails subjects by the inimitable Geoffrey Grosenbach, available at http://peepcode.com

Ryan Bates does an excellent job explaining nuances of Rails development in his long-running series of free webcasts available at http://railscasts.com/

Regarding David Heinemeier Hansson a.k.a. DHH. I had the pleasure of establishing a friendship with David, creator of Rails, in early 2005, before Rails hit the mainstream and he became an International Web 2.0 Superstar. My friendship with David is a big factor in why I’m writing this book today. David’s opinions and public statements shape the Rails world, which means he gets quoted a lot when we discuss the nature of Rails and how to use it effectively.

As of 2013, I don’t know if this is true anymore, but back when I wrote the original edition of this book, David had told me on a couple of occasions that he hates the “DHH” moniker that people tend to use instead of his long and difficult-to-spell full name. For that reason, in this book I try to always refer to him as “David” instead of the ever-tempting “DHH.” When you encounter references to “David” without further qualification, I’m referring to the one-and-only David Heinemeier Hansson.

There are a number of notable people from the Rails world that are also referred to on a first-name basis in this book. Those include:

· Yehuda Katz

· Jamis Buck

· Xavier Noria

· Tim Pope

Goals

As already stated, I hope to make this your primary working reference for Ruby on Rails. I don’t really expect too many people to read it through end to end unless they’re expanding their basic knowledge of the Rails framework. Whatever the case may be, over time I hope this book gives you as an application developer/programmer greater confidence in making design and implementation decisions while working on your day-to-day tasks. After spending time with this book, your understanding of the fundamental concepts of Rails coupled with hands-on experience should leave you feeling comfortable working on real-world Rails projects, with real-world demands.

If you are in an architectural or development lead role, this book is not targeted to you, but should make you feel more comfortable discussing the pros and cons of Ruby on Rails adoption and ways to extend Rails to meet the particular needs of the project under your direction.

Finally, if you are a development manager, you should find the practical perspective of the book and our coverage of testing and tools especially interesting, and hopefully get some insight into why your developers are so excited about Ruby and Rails.

Prerequisites

The reader is assumed to have the following knowledge:

· Basic Ruby syntax and language constructs such as blocks

· Solid grasp of object-oriented principles and design patterns

· Basic understanding of relational databases and SQL

· Familiarity with how Rails applications are laid out and function

· Basic understanding of network protocols such as HTTP and SMTP

· Basic understanding of XML documents and web services

· Familiarity with transactional concepts such as ACID properties

As noted in the section “Book Structure,” this book does not progress from easy material in the front to harder material in the back. Some chapters do start out with fundamental, almost introductory material, and push on to more advanced coverage. There are definitely sections of the text that experienced Rails developer will gloss over. However, I believe that there is new knowledge and inspiration in every chapter, for all skill levels.

Required Technology

A late-model Apple MacBookPro running Mac OS X. Just kidding, of course. Linux is pretty good for Rails development also. Microsoft Windows—well, let me just put it this way—your mileage may vary. I’m being nice and diplomatic in saying that. We specifically do not discuss Rails development on Microsoft platforms in this book. It’s common knowledge that the vast majority of working Rails professionals develop and deploy on non-Microsoft platforms.